gpt4 book ai didi

c# - WebAPI 上传错误。 MIME 多部分流的预期结束。 MIME 多部分消息不完整

转载 作者:太空狗 更新时间:2023-10-29 20:17:10 26 4
gpt4 key购买 nike

我一直按照本教程支持文件上传作为 MVC4 的 WebAPI 的一部分:http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx .

当我的 Controller 收到请求时,它会提示“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>

我的 Controller 的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com