tags:

views:

345

answers:

1

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

+2  A: 

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:

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.

Justin Grant
Oh no! Your link to blog, point to this post, instead of actual blog entry. Can you please fix it?
SuitUp
oops, sorry, fixed now.
Justin Grant
I think this solution is not good for me, because i send every file in separate POST and without UploadValues i can't send any more information. Can you help me send files in one POST and with additional data?
SuitUp
OK, now I understand what you're trying to do. I revised my answer, which should be what you're looking for.
Justin Grant
I'm sad i can't give you more reputation points. As soon i will finish choosing one of your links, i will mark this as an answer. Or i just mark it now. ;]
SuitUp