I got this question a couple of weeks ago but just never had the time to put into answering fully. But today I have some extra time due to the fact that I’m under-booked with projects.
The question went something like this:
“I want to be able to upload a file from a desktop application to a web site that has a form that accepts the file as a post. How do I do that?”
And while I’ve done some things in the past that come close, I’ve never had to do this exact task. But it does look interesting.
The entire trick to making this work is to make the web server think that the form has been filled out and that the post has been implemented.
To do this, you’ll need to create a WebRequest object and set some properties on it. The two that you must set are the Method property (to POST) and the ContentType (to “multipart/form-data; boundary=”+boundary). If you are sending multiple files, you’ll need multiple boundaries.
System.Net.WebRequest wr = System.Net.WebRequest.Create("url goes here"); wr.ContentType = "multipart/form-data; boundary=xyz"; wr.Method = "POST";
To send data, we need to format the data in multipart/form-data mode. First we retrieve the file into a stream.
FileStream fs= new FileStream("your file here", System.IO.FileMode.Open, System.IO.FileAccess.Read); byteArray= new byte[loFile.Length]; fs.Read(byteArray,0,(int) byteArray.Length); fs.Close();
Then we setup the multipart boundary header.
string var = "--" + boundaryID + "\r\n" + "Content-Disposition: form-data; name=\"" + Key + "\" filename=\"" + new FileInfo("your file here").Name + "\"\r\n\r\n";
Then we write the boundary header and the content to the output stream
System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms); bw.Write(Encoding.GetEncoding(1252).GetBytes(var)); bw.Write(byteArray); bw.Write(Encoding.GetEncoding(1252).GetBytes("\r\n")); System.IO.Stream postStream = wr.GetRequestStream(); ms.WriteTo(postStream); postStream.Close();
Note the last bw.Write() line is telling the server this is the end of the boundary.
And then the final bit of code that sends it to the server and gets the resulting HTML.
System.Net.WebResponse wresponse = wr.GetResponse(); System.Text.Encoding enc = System.Text.Encoding.GetEncoding(1252); System.IO.StreamReader responseStream = new System.IO.StreamReader (wresponse.GetResponseStream(), enc); string lcHtml = responseStream.ReadToEnd(); wresponse.Close(); responseStream.Close();
That’s all there is to it. Once you know how to do it, it is rather simple.
Other places talking about WebRequest using multipart/form-data
Brian Grinstead » Blog Archive » Multipart Form Post in C# – So, I needed to roll my own form post. Here is the Multipart Form RFC and the W3C Specification for multipart/form data. After reading these links and searching some forums, here is what I came up with. ….. userAgent, contentType, formData) End Function Private Function PostForm(ByVal postUrl As String, _ ByVal userAgent As String, _ ByVal contentType As String, _ ByVal formData As Byte()) _ As HttpWebResponse Try Dim request As HttpWebRequest = WebRequest. …
Retrieving HTTP content in .NET with WebRequest/WebResponse – This code finalizes the request for POST data by checking whether we’ve already written something into the POST buffer and if so configuring the POST request by specifying the content type. In the case of multi-part form POST an …
Other post in c#
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Simple Properties in C# 3.5 - November 22nd, 2007
- CSharp adds the var keyword! - November 27th, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- Computed Columns Using LINQ to SQL Classes - February 12th, 2008
- DataSets, ObjectDataSource and 3-Tier ASP.NET - February 29th, 2008
- Serializable attribute vs Serializable Interface - April 9th, 2008
- Why you can't cast an integer to a string. - June 23rd, 2008
- Unsafe Mode in C# - December 15th, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- Upload a File via WebRequest Using CSharp - November 10th, 2009
- Azure CDN Blob Storage Case Sensitive Issue - April 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Decimal vs Float (Single) or Double - September 18th, 2012
- C# Self Executing Anonymous Function - February 28th, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
- CSharp Enum - October 2nd, 2013
- CSharp Numeric Overflows - July 3rd, 2014
- CSharp IDisposable Confusion - July 17th, 2014
- Why Get Certified? - July 24th, 2014
- C# Properties Get and Set - November 13th, 2014
- Value Type vs Reference Type - November 27th, 2014
- String and StringBuilder - December 11th, 2014
- Browser Automation in .NET w/ Chromium - July 9th, 2015
- CefSharp Offscreen [Why do I have so many instances of Chromium?] - October 1st, 2015