7

我一直在关注本教程以支持将文件上传作为 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!"));
        }
    }

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

4

1 回答 1

8

您可以尝试将“名称”属性添加到您的输入文件吗?

<input name="file1" id="file1" type="file" multiple="multiple" />
于 2013-12-17T20:50:54.920 回答