如何上传文件到Asp.Net MVC 4.0的动作在IIS防爆preSS运行与HttpClient的包括在.NET 4.0级 [英] How to upload files to Asp.Net MVC 4.0 action running in IIS Express with HttpClient class included in .Net 4.0
问题描述
我有一个C / S应用。我实现了服务器Asp.Net MVC 4.0,并且在客户端上.NET 4.5中运行。
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
...
}
}
...
}
我创建了一个测试的HTML页面来测试它在浏览器中。它的工作预期。
I created a test html page to test it in browser. It worked as expected.
在客户端,我使用的HttpClient 一>类,它完美地工作没有文件上传涉及在哪里。但是研究的天后,我仍然没有运气在我的机器调试解决这个它运行IIS防爆preSS。我发现所有的线索导致 MultipartFormDataContent
,但仍无法得到它的工作,即使我复制这些样品codeS,在服务器端仍然无法获得一件事,所有的ARGS是空的,并且没有文件是存在于 Request.Files
。我使用了基于的HttpWebRequest 一个我自己的HTTP辅助类有>,它适用于文件上传,但我preFER使用的HttpClient在这个新项目。
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.
所以,我如何上传文件到服务器与HttpClient的?
So, How do I upload files to the server with HttpClient?
推荐答案
与调试小提琴手,与比较的WinMerge原始HTTP消息后,我发现Firefox和我的计划之间的差异:
After debugging with Fiddler, comparing raw http message with WinMerge, I found the differences between Firefox and my program:
火狐(去掉一些头把事情simpe):
POST http://localhost:53400/Input/Upload HTTP/1.1
Host: localhost:53400
Content-Type: multipart/form-data; boundary=---------------------------1590871622043
Content-Length: ****
-----------------------------1590871622043
Content-Disposition: form-data; name="arg1"
abc
-----------------------------1590871622043
Content-Disposition: form-data; name="arg2"
3
-----------------------------1590871622043
Content-Disposition: form-data; name="uploadfile"; filename="wave.wav"
Content-Type: audio/wav
//file data here
-----------------------------1590871622043--
我有计划 MultipartFormDataContent
:
My Program with MultipartFormDataContent
:
POST http://localhost:53400/Input/Save HTTP/1.1
Content-Type: multipart/form-data; boundary="caac5ea7-8ab4-4682-be40-ecb3ddf3e70a"
Host: localhost:53400
Content-Length: ****
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a
Content-Disposition: form-data; name=arg1
abc
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a
Content-Disposition: form-data; name=arg2
3
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a
Content-Disposition: form-data; name=uploadfile; filename=wave.wav; filename*=utf-8''wave.wav
//file data here
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a--
我会注意到的最后一件事是,在这些内容处置
线,火狐引用的所有值,但我的程序没有。人们可以很容易地认为它不会有问题,但最后,我发现这是确切的原因。
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.
现在,我知道原因,来了code是,工作容易,因为引用的名称:
Now that I know the reason, here comes the code that works, as easy as quoting the names:
var multipart = new MultipartFormDataContent();
multipart.Add(new StringContent("abc"), '"' + "arg1" + '"');
multipart.Add(new StringContent("3"), '"' + "arg2" + '"');
// byte[] fileData;
multipart.Add(new ByteArrayContent(fileData), '"' + "uploadfile"+ '"', '"' + "wave.wav" + '"');
//HttpClient http; string url;
var response = await http.PostAsync(url, multipart);
response.EnsureSuccessStatusCode();
//...
这篇关于如何上传文件到Asp.Net MVC 4.0的动作在IIS防爆preSS运行与HttpClient的包括在.NET 4.0级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!