我一直遵循本教程来支持作为MVC4 WebAPI的一部分的文件上传:http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx。
当我的控制器收到请求时,它会抱怨“mime多部分消息不完整”。有人知道如何调试这个程序吗?我已经尝试将流的位置重置为0,以防在它击中处理程序之前有其他东西正在读取它。
我的HTML如下所示:
<form action="/api/giggl" method="post" enctype="multipart/form-data">
<span>Select file(s) to upload :</span>
<input id="file1" type="file" multiple="multiple" />
<input id="button1" type="submit" value="Upload" />
</form>
我的控制器的post方法如下:
public Task<IEnumerable<string>> Post()
{
if (Request.Content.IsMimeMultipartContent())
{
Stream reqStream = Request.Content.ReadAsStreamAsync().Result;
if (reqStream.CanSeek)
{
reqStream.Position = 0;
}
string fullPath = HttpContext.Current.Server.MapPath("~/App_Data");
var streamProvider = new MultipartFormDataStreamProvider(fullPath);
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled)
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
var fileInfo = streamProvider.FileData.Select(i =>
{
var info = new FileInfo(i.LocalFileName);
return "File uploaded as " + info.FullName + " (" + info.Length + ")";
});
return fileInfo;
});
return task;
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!"));
}
}
我遗漏了什么明显的东西吗?>
最佳答案
你能试着在输入文件中添加“name”属性吗?
<input name="file1" id="file1" type="file" multiple="multiple" />
关于c# - WebAPI上传错误。 MIME多部分流的预期结尾。 MIME分段消息不完整,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19619100/