Connecting Tech Pros Worldwide Help | Site Map

How to upload multiple files using HttpWebRequest in c#?

Newbie
 
Join Date: Jan 2008
Posts: 17
#1: May 20 '08
How to upload multiple files at a time using HttpWebRequest in c#?

thanks
Abdun Nabi Sk
Newbie
 
Join Date: May 2008
Posts: 6
#2: May 21 '08

re: How to upload multiple files using HttpWebRequest in c#?


Quote:

Originally Posted by abdunnabisk

How to upload multiple files at a time using HttpWebRequest in c#?

thanks
Abdun Nabi Sk

You can use WebClient.FileUpload method to send all files one by one to the server. Shortcoming: one file is sent in one request.

You can use third party components (ex:Chilkat.Upload)

Use can also write your own class, that based on HttpWebRequest. This class should implement RFC 1867 (www.ietf.org/rfc/rfc1867.txt). In this case you will need to build http request and send it to request stream.
Newbie
 
Join Date: Jan 2008
Posts: 17
#3: May 21 '08

re: How to upload multiple files using HttpWebRequest in c#?


Thansks for your reply.I want to upload more than one file using httpwebrequest in a single transaction and not one by one.Can you provide me some codes on that.

Thanks
Abdun nabi sk

Quote:

Originally Posted by GregoryPankov

You can use WebClient.FileUpload method to send all files one by one to the server. Shortcoming: one file is sent in one request.

You can use third party components (ex:Chilkat.Upload)

Use can also write your own class, that based on HttpWebRequest. This class should implement RFC 1867 (www.ietf.org/rfc/rfc1867.txt). In this case you will need to build http request and send it to request stream.

Newbie
 
Join Date: Jan 2008
Posts: 17
#4: May 21 '08

re: How to upload multiple files using HttpWebRequest in c#?


Finally I got this done...here is the code

string boundary = "----------------" +
DateTime.Now.Ticks.ToString("x");

string url = ConfigurationManager.AppSettings["ServerAddress"];
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = fileContentType+";+ boundary=" + boundary;
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.Timeout = 600000;
httpWebRequest.ReadWriteTimeout = 600000;
//httpWebRequest.AllowWriteStreamBuffering = true;
httpWebRequest.Credentials=System.Net.CredentialCa che.DefaultCredentials;

Stream memStream = new System.IO.MemoryStream();

byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
memStream.Write(boundarybytes, 0, boundarybytes.Length);

string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type:" + defaultContentType + "\r\n\r\n";

for(int i=0;i<files.Length;i++)
{
string header = string.Format(headerTemplate,"file"+i,new FileInfo(files[i]).Name);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes,0,headerbytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;

while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 )
{
memStream.Write(buffer, 0, bytesRead);
}

memStream.Write(boundarybytes,0,boundarybytes.Leng th);
fileStream.Close();
}

httpWebRequest.ContentLength = memStream.Length;

Stream requestStream = httpWebRequest.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();

try
{
WebResponse webResponse = httpWebRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
Console.WriteLine(reader.ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
}
httpWebRequest = null;

Quote:

Originally Posted by abdunnabisk

Thansks for your reply.I want to upload more than one file using httpwebrequest in a single transaction and not one by one.Can you provide me some codes on that.

Thanks
Abdun nabi sk

Newbie
 
Join Date: Oct 2009
Posts: 1
#5: 4 Weeks Ago

re: How to upload multiple files using HttpWebRequest in c#?


That's great! But how do we actually receive the multiple files? Do you have any code sample for the receiving URL?

Thanks.

Regards,
Azman
Reply


Similar .NET Framework bytes