我使用的是 Visual Studio 2012 Express 附带的 MVC 版本。 (微软.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 结尾t.IsFaulted == true
。异常内容如下:
MIME 多部分流意外结束。 MIME 多部分消息不是
完全的。
这是我的客户表格。没什么花哨的,我想做 jquery 表单插件来进行 ajax 上传,但我什至无法让这种方式工作。
<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 中却不起作用?
你缺少一个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>
没有它的输入将不会被浏览器提交。所以你的表单数据是空的导致IsFaulted
被断言。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)