上载分段媒体文件时FileData为空

时间:2018-12-30 10:53:06

标签: c# asp.net file-upload asp.net-mvc-5 asp.net-web-api2

我使用 Postman 和我自己的基于Restsharp的客户端将音频文件发送到Web API控制器,当大小超过3或4 MB提供商时.FileData显示包含0个文件

我也尝试使用此方法,但无法解决问题。      var provider =新的MultipartFileStreamProvider(root);

    public async Task<HttpResponseMessage> UploadFile()
    {
        HttpRequestMessage request = this.Request;
        if (!request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/uploads");
        var provider = new MultipartFormDataStreamProvider(root);

        var task = await request.Content.ReadAsMultipartAsync(provider).
            ContinueWith<HttpResponseMessage>(o =>
                {
                    FileInfo finfo = new FileInfo(provider.FileData.First().LocalFileName);
                    string guid = Guid.NewGuid().ToString();
                    string fileName = guid + "_" + provider.FileData.First().Headers.ContentDisposition.FileName
                                          .Replace("\"", "");
                    File.Move(finfo.FullName, Path.Combine(root, fileName));

                    return new HttpResponseMessage()
                    {
                        Content = new StringContent("File uploaded.")
                    };
                }
            );

        return task;
    }

0 个答案:

没有答案