473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to upload multiple files using HttpWebRequest in c#?

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

thanks
Abdun Nabi Sk
May 20 '08 #1
4 17351
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.
May 21 '08 #2
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

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.
May 21 '08 #3
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;

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
May 21 '08 #4
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
Oct 22 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rajesh S | last post by:
Hi, Is it possible to upload multiple files using WebClient Class(Like HttpRequest Class).? If Possible please help!!
4
by: R Reyes | last post by:
I am trying to code a file uploader (for forum/email attachments) from the client computer to a remote web server via the PUT method (since POST is not allowed ). However, the upload works ONLY...
5
by: John Lee | last post by:
Hi, I have a simple web page that allow file to be uploaded, the upload page looks like the following: <form method="post" name="upload" enctype="multipart/form-data"...
2
by: Jonathan Wax | last post by:
Hi, I spent the last week looking for an answer to the following question: How can you upload an xml file to an HTTPS server with a specific certificate. Basically doing the same as this html...
7
by: pbd22 | last post by:
hi. i am having probs understanding how to grab a file being uploaded from a remote client. i am using hidden input fields for upload such as: <input id="my_file_element" type="file"...
4
by: Spam Catcher | last post by:
Anyone know of any form upload utilities which will allow me to upload files to an ASP.NET page without the need of a web browser? I like to test a form upload page I wrote (no GUI, it only...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.