Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload multiple files using webclient UploadFile, UploadValues in C#?

Tags:

c#

webclient

How to upload multiple files using webclient UploadFile, UploadValues in C#?

like image 232
SuitUp Avatar asked Jun 01 '10 13:06

SuitUp


1 Answers

This blog post details exactly how to upload multiple files using WebClient.

If you want to upload both form fields and a file in the same POST, you can't use WebClient as-is-- instead it will need to be extended. Here's an excerpt from this article explaining what is needed:

the only option is to create a custom implementation that conforms to rfc1867, rfc2388 and the W3C multipart/form-data specification that will enable file upload with additional form fields and exposes control of cookies and headers.

Here are three implementations, using slightly different approaches, but all should work to enable multi-part form data:

  • http://www.codeproject.com/Articles/72232/Csharp-File-Upload-with-form-fields-cookies-and-he.aspx
  • http://www.codeproject.com/KB/cs/uploadfileex.aspx
  • http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx

WebClient.UploadValues is not designed to upload files-- instead it's used to send POST-ed form values to the server. You want to use WebClient.UploadFile to upload files, or one of the advanced samples above.

like image 111
Justin Grant Avatar answered Oct 19 '22 19:10

Justin Grant