Jay*_*012 8 .net-4.0 asp.net-mvc-4 asp.net-web-api
我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码总是以continueWith结束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>
Run Code Online (Sandbox Code Playgroud)
我已经读过它是由每个消息末尾的解析器期望/ CR/LF引起的,并且该错误已在6月修复.
我无法弄清楚的是,如果真的修复了,为什么不包括这个版本的MVC 4?为什么互联网上有这么多例子说这个代码在这个版本的MVC 4中不起作用?
小智 19
您缺少name
文件中的属性input
.
<form name="uploadForm" method="post" enctype="multipart/form-data" action="api/upload" >
<input name="myFile" type="file" />
<input type="submit" value="Upload" />
</form>
Run Code Online (Sandbox Code Playgroud)
没有它的输入将不会被浏览器提交.所以你的formdata是空的,导致IsFaulted
被断言.
归档时间: |
|
查看次数: |
10647 次 |
最近记录: |