how can i upload all types of fule without using asp.net's fileupload control?
Hi,i want to know about how i can upload all types of file without using the fileupload control of asp.net.
i had written code of both server and client but it only upload the text files only..
here i am describing that code..
hope u help me to make my minor project better.
here i had used Base64 encoding and decoding which i had done to upload all types of file but i can't do as per my requirement..
i want to upload all types of files like (.RAR,.EXE,.BIN..etc) on the server..
hope i will get valuable help from this website..
client code:
using System;
using System.Web;
using System.Web.Configuration;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Client : System.Web.UI.Page
{
Socket m_clientSocket, m_handler;
IAsyncResult m_result;
int cn=1;
byte[] a ;
byte[] a1,b1;
byte[] b;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
connect();
// disconnect();
}
public void disconnect()
{
try
{
if (m_clientSocket != null)
{
m_clientSocket.Close();
m_clientSocket = null;
//UpdateControls(false);
string n;
}
}
catch (SocketException se)
{
Response.Write("Connection failed" + se.Message.ToString());
}
}
public void connect()
{
try
{
if(cn==1)
{
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPAddress A = IPAddress.Parse("10.1.7.88");
IPAddress[] aryLocalAddr = null;
string HostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(HostName);
aryLocalAddr = ipEntry.AddressList;
int nPort =12533;
// IPEndPoint ipEnd = new IPEndPoint(A, nPort);
IPEndPoint ipEnd = new IPEndPoint(aryLocalAddr[0], nPort);
m_clientSocket.Connect(ipEnd);
cn = 0;
}
if (m_clientSocket.Connected)
{
// UpdateControls(true);
//Wait for data asynchronously
SendFilename();
WaitForData();
//WaitForData();
//Timer a = new Timer();
// SendData();
// WaitForData();
// WaitForData();
//WaitForData(d);
}
}
catch (SocketException se)
{
Response.Write(se.Message);
}
}
public void SendFilename()
{
if (fupload.HasFile)
{
try
{
string fname=fupload.FileName;
a1 = new byte[fname.Length];
a1=System.Text.ASCIIEncoding.ASCII.GetBytes(fname);
string returnValue= System.Convert.ToBase64String(a1);
a = System.Text.Encoding.ASCII.GetBytes(returnValue);
NetworkStream ns = new NetworkStream(m_clientSocket);
//ns.ReadTimeout = 1000000000;
ns.Flush();
ns.Write(a, 0, a.Length);
//ns.WriteTimeout = 1000000000;
ns.Dispose();
ns.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
public void SendData()
{
if (fupload.HasFile)
{
try
{
b1 = new byte[fupload.PostedFile.ContentLength];
// System.IO.StreamReader s=new System.IO.StreamReader(FileUpload1.FileName);
fupload.FileContent.Read(b1, 0, fupload.PostedFile.ContentLength);
string name = System.Text.Encoding.ASCII.GetString(b1);
a1 = System.Text.ASCIIEncoding.ASCII.GetBytes(name);
string returnValue = System.Convert.ToBase64String(a1);
a = System.Text.Encoding.ASCII.GetBytes(returnValue);
NetworkStream n = new NetworkStream(m_clientSocket);
// n.ReadTimeout = 1000000000;
n.Flush();
n.Write(a, 0,a.Length);
/* if (m_clientSocket != null)
{
NetworkStream n = new NetworkStream(m_clientSocket);
n.Write(a,0,FileUpload1.PostedFile.ContentLength);
}
string name=System.Text.Encoding.ASCII.GetString(a);
Response.Write(name);*/
//n.Dispose();
n.Dispose();
n.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message + "");
}
}
else
{
Response.Write("Not Specified file");
}
}
public void WaitForData()
{
SocketPacket theSocPkt = new SocketPacket();
theSocPkt.m_currentSocket = m_clientSocket;
m_result = m_clientSocket.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), theSocPkt);
}
public void OnReceive(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.m_currentSocket.EndReceive(asyn);
string ans = System.Text.Encoding.ASCII.GetString(theSockId.dataBuffer, 0, iRx);
byte[] encodedDataAsBytes= System.Convert.FromBase64String(ans);
string returnValue =System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
if ((returnValue.CompareTo("Send Data"))==0)
{
SendData();
}
else if ((returnValue.CompareTo("Got Data")) == 0)
{
// Response.Write("Congratulation Your File Has been uploaded");
// disconnect();
Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\"> alert('please fill up all the records');</script>");
//Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\"> alert('please fill up all the records');</script>");
}
WaitForData();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
server's code:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class FServer : System.Web.UI.Page
{
private Socket m_mainSocket;
private Socket[] m_workerSocket = new Socket[10];
private int m_clientCount = 0;
private int[] count = new int[10];
byte []a1;
byte[] a2;
string[] fname = new string[10];
protected void Page_Load(object sender, EventArgs e)
{
int i;
for(i=0;i<10;i++)
{
count[i] = 0;
}
}
protected void btnStart_Click(object sender, EventArgs e)
{
IPAddress[] aryLocalAddr = null;
string HostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(HostName);
aryLocalAddr = ipEntry.AddressList;
int nPort = 12533; /*any port which is available*/ ;
// Create the listener socket
m_mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_mainSocket.Bind(new IPEndPoint(aryLocalAddr[0], nPort));
m_mainSocket.Listen(10);// Setup a callback routine for connection notification.
m_mainSocket.BeginAccept(new AsyncCallback(OnConnect), m_mainSocket);
UpdateControls(true);
}
private void UpdateControls(bool listening)
{
btnStart.Enabled = !listening;
btnStop.Enabled = listening;
}
public void OnConnect(IAsyncResult asyn)
{
try
{
Socket listener = (Socket)asyn.AsyncState;
m_workerSocket[m_clientCount] = listener.EndAccept(asyn);
// Let the worker Socket do the further processing for the
// just connected client
WaitForData(m_workerSocket[m_clientCount]);
// Now increment the client count
count[m_clientCount] = 1;
++m_clientCount;
// Session["clientCount"] = m_clientCount;
//Session.Add("clientCount", m_clientCount);
listener.BeginAccept(new AsyncCallback(OnConnect), listener);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
Response.Write(se.Message);
//MessageBox.Show(se.Message);
}
}
public void WaitForData(Socket soc)
{
try
{
SocketPacket theSocPkt = new SocketPacket();
theSocPkt.m_currentSocket = soc;
// theSocPkt.dataBuffer = new byte[theSocPkt.m_currentSocket.Available];
// Start receiving any data written by the connected client
// asynchronously
soc.BeginReceive(theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length,
SocketFlags.None,
new AsyncCallback(OnReceive),
theSocPkt);
}
catch (SocketException se)
{
Response.Write(se.Message);
}
}
public void OnReceive(IAsyncResult asyn)
{
try
{
SocketPacket thePacket = (SocketPacket)asyn.AsyncState;
int i = thePacket.m_currentSocket.EndReceive(asyn);
//thePacket.dataBuffer = new byte[i];
string name1 = System.Text.Encoding.ASCII.GetString(thePacket.dataBuffer, 0, i).ToString();
byte[] encodedDataAsBytes = System.Convert.FromBase64String(name1);
string name = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
//string name = System.Text.Encoding.ASCII.GetString(thePacket.dataBuffer,0,i).ToString();
//char[] chars = new char[i + 1];
//System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
//int charLen = d.GetChars(thePacket.dataBuffer, 0, i, chars, 0);
//System.String szData = new System.String(chars);
if (i > 0)
{
for(int j=0;j<m_clientCount;j++)
{
if (count[m_clientCount-1]==1)
{
FileStream fs = new FileStream("E:\\upload\\" + name, FileMode.CreateNew);
fname[m_clientCount] = name;
count[m_clientCount-1] = 2;
fs.Dispose();
fs.Close();
NetworkStream n = new NetworkStream(thePacket.m_currentSocket);
string send="Send Data";
byte[] toEncodeAsBytes= System.Text.ASCIIEncoding.ASCII.GetBytes(send);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
byte[] a=new byte[returnValue.Length];
a=System.Text.Encoding.ASCII.GetBytes(returnValue);
n.Write(a, 0, a.Length);
n.Dispose();
n.Close();
// WaitForData(thePacket.m_currentSocket);
}
else if (count[m_clientCount-1] == 2)
{
FileStream fs = new FileStream("E:\\upload\\"+fname[m_clientCount],FileMode.Open,FileAccess.Write,FileShare.None);
a1 = new byte[i];
a1 = System.Text.ASCIIEncoding.ASCII.GetBytes(name);
//string abc= System.Text.Encoding.ASCII.GetString(a1, 0, i).ToString();
//a2 = new byte[abc.Length];
//a2 = System.Text.Encoding.ASCII.GetBytes(abc);
fs.Write(a1,0,a1.Length);
fs.Dispose();
fs.Close();
NetworkStream n = new NetworkStream(thePacket.m_currentSocket);
string send = "Got Data";
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(send);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
byte[] a = new byte[returnValue.Length];
a = System.Text.Encoding.ASCII.GetBytes(returnValue);
n.Write(a, 0, a.Length);
n.Dispose();
n.Close();
count[m_clientCount] = 0;
// WaitForData(thePacket.m_currentSocket);
}
}
}
WaitForData(thePacket.m_currentSocket);
}
catch (Exception se)
{
Response.Write(se.Message);
}
}
protected void btnStop_Click(object sender, EventArgs e)
{
CloseSockets();
UpdateControls(false);
}
void CloseSockets()
{
int count=Convert.ToInt32(Session["clientCount"]);
if (m_mainSocket != null)
{
m_mainSocket.Close();
}
for (int i = 0; i < count; i++)
{
if (m_workerSocket[i] != null)
{
m_workerSocket[i].Close();
m_workerSocket[i] = null;
}
}
}
}