Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had to do the same thing on a project last year. After some looking around, I found this: </p> <p><a href="https://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data/2996904#2996904">Upload files with HTTPWebrequest (multipart/form-data)</a></p> <p>The second answer in there should be what you're looking for. When I was trying to do this I ran into some trouble getting that exact method to work. The problem is that C#.NET does not have any support for multiple key/value pairs in a POST. So you must construct the HTTP request content header yourself. I believe the answer in the link above writes it directly to the request stream. I was able to convert the code found at the link below to build the header, and then wrote the bytes to the request stream.</p> <p><a href="http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/" rel="nofollow noreferrer">http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/</a></p> <p>My code looked something like the following (I've had to modify it below for readability because a lot of abstracted out as part of the whole project, so it may not be perfect syntactically).</p> <pre><code> public void BuildHeader(string contentType, string filename) { if ((contentType == null) || (contentType.Length == 0)) { contentType = "application/octet-stream"; } // Create the boundary string for the POST message header string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); // Build up the POST message header StringBuilder sb = new StringBuilder(); // The specific format used can be found in the HTTP protocol specs. // The 'name' variable indicates the field-name, and the last variable // added to the string before another boundary is the value for that field. sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("path"); sb.Append("\""); sb.Append("\r\n\r\n"); sb.Append(fileName); sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("contents"); sb.Append("\"; fileName=\""); sb.Append("abc"); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append(contentType); sb.Append("\r\n"); sb.Append("\r\n"); using (Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { // Add the file contents to the POST message byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { sb.Append(ASCIIEncoding.ASCII.GetString(buffer)); } // Get the byte array of the POST message, and its length string totalContents = sb.ToString(); byte[] totalUpload = Encoding.UTF8.GetBytes(totalContents); int length = totalUpload.Length; } } </code></pre> <p>Note that this only prepares a header for the upload of a single file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload