0

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)

1
  • I don't think it's a dupe of that question. The OP wants an easy method to accomplish it (without the hassles of HttpWebRequest). Commented Apr 20, 2009 at 11:04

2 Answers 2

3

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

var client = new WebClient();
client.UploadFile("http://server/upload.aspx", @"C:\file.jpg");
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.