【发布时间】:2012-10-31 20:07:06
【问题描述】:
我正在使用随 Visual Studio 2012 express 提供的 MVC 版本。 (Microsoft.AspNet.Mvc.4.0.20710.0)
我假设这是 RTM 版本。
我在网上找到了很多都使用这个代码的例子:
public Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
});
return task;
}
但是这段代码总是在 continueWith where t.IsFaulted == true 结束。异常内容如下:
MIME 多部分流意外结束。 MIME 多部分消息不是 完成。
这是我的客户表格。没什么花哨的,我想做 ajax 上传的 jquery 表单插件,但我什至无法用这种方式工作。
<form name="uploadForm" method="post" enctype="multipart/form-data" action="api/upload" >
<input type="file" />
<input type="submit" value="Upload" />
</form>
我了解到这是由于解析器在每条消息的末尾都期望 /CR /LF 引起的,并且该错误已在 6 月修复。
我想不通的是,如果它真的被修复了,为什么它不包含这个版本的 MVC 4?为什么互联网上有这么多例子吹捧这段代码在这个版本的 MVC 4 中不起作用?
【问题讨论】:
-
ASP.NET Web API 不是 ASP.NET MVC 的一部分,它有自己的 NuGet 包 --> ASP.NET Web API 4.0.20710.0。请检查您使用的是哪个版本。
标签: .net-4.0 asp.net-mvc-4 asp.net-web-api