WebAPI上传错误.MIME多部分流的预期结束.MIME多部分消息未完成

tri*_*son 7 c# asp.net file-upload asp.net-mvc-4 asp.net-web-api

作为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>
Run Code Online (Sandbox Code Playgroud)

和我的控制器的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!"));
        }
    }
Run Code Online (Sandbox Code Playgroud)

我错过了什么明显的吗?>

Dal*_*rzo 8

您可以尝试在输入文件中添加"name"属性吗?

<input name="file1" id="file1" type="file" multiple="multiple" />
Run Code Online (Sandbox Code Playgroud)