I have a C/S application. I implemented the server with Asp.Net MVC 4.0, and the client runs on .Net 4.5.
I have a Controller Action in the server side looks like this:
public JsonResult Upload(string arg1, int arg2)
{
//do something about arg1 and arg2 here
...
var files = Request.Files;
if (files.Count > 0)
{
foreach(var file in files)
{
var ms = new MemoryStream();
file.InputStream.CopyTo(ms);
//save it to somewhere
...
}
}
...
}
I created a test html page to test it in browser. It worked as expected.
In the client side, I use HttpClient class and it worked perfectly where no file uploading involved. However after days of researching I still have no luck to resolve this in my debug machine which runs IIS Express. I found that all clues leads to MultipartFormDataContent
, but still can't get it work, even if I copy those sample codes, the server side still can't get a thing, all args are empty, and no files is there in the Request.Files
. I used to have my own http helper class based on HttpWebRequest, which works for file uploading, but I prefer to use HttpClient in this new project.
So, How do I upload files to the server with HttpClient?
After debugging with Fiddler, comparing raw http message with WinMerge, I found the differences between Firefox and my program:
Firefox (removed some headers to make things simpe):
My Program with
MultipartFormDataContent
:The last thing I would notice is that in these
Content-Disposition
lines, Firefox quotes all values, but my program does not. One could easily assume that it would not matter, but in the end, I found that is the exact cause.Now that I know the reason, here comes the code that works, as easy as quoting the names: