views:

585

answers:

2

C# provides functionality to submit a post request, but there is nothing about uploading an image/file on MSDN. I'd like to do this without using raw headers.

Related questions

Upload files with HTTPWebrequest (multipart/form-data)

+2  A: 

You can use WebClient class easily. It has an UploadFile method:

var client = new WebClient();
client.UploadFile("http://server/upload.aspx", @"C:\file.jpg");
Mehrdad Afshari
I'd like to upload a file and submit other post variables at the same time. Any idea how I'd do that?
In that case, I'm afraid your question is essentially a dupe of the one linked above.
Mehrdad Afshari
The one linked above doesn't include post variables. UploadFile() doesn't let me specify any other form values (name, board) which need to be set in order to upload the image.
+1  A: 

My ASP.NET Upload FAQ has an article on this, with example code: Upload files using an RFC 1867 POST request with HttpWebRequest/WebClient. This code doesn't load files into memory, supports multiple files, and supports form values, setting credentials and cookies, etc.

Chris Hynes