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)

Community
  • 1
  • 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). – Mehrdad Afshari Apr 20 '09 at 11:04

2 Answers2

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");
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • I'd like to upload a file and submit other post variables at the same time. Any idea how I'd do that? –  Apr 20 '09 at 15:42
  • In that case, I'm afraid your question is essentially a dupe of the one linked above. – Mehrdad Afshari Apr 20 '09 at 15:58
  • 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. –  Apr 21 '09 at 15:58
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.

Chris Hynes
  • 9,999
  • 2
  • 44
  • 54