Web API:multipart / form-data post无法正常工作

时间:2016-11-04 18:59:31

标签: c# asp.net-web-api async-await streaming fiddler

我已阅读过每篇网络帖子,但我仍然不知道自己做错了什么。从Fiddler开始,我正在尝试发布多个项目但它无效。我无法发布.jpeg,但.txt无效。对该方法的第一次请求是成功的,但所有后续请求都不会,并且将生成“ MIME多部分流的意外结束.MIME多部分消息未完成”或“异步模块或处理程序在异步操作仍处于等待状态时完成“。如果我只是在部分/边界中发送文本条目,它将只看到第一个 - 其他被忽略

Web API

[HttpPost]
[MimeMultipart]
public async Task<IHttpActionResult> PostAsync()
     {
      var uploadPath = HttpContext.Current.Server.MapPath("~/Uploads");
      Directory.CreateDirectory(uploadPath);
      var provider = new MultipartFormDataStreamProvider(uploadPath);
      // THIS IS WHERE THE ERRORS HAPPEN
      await Request.Content.ReadAsMultipartAsync(provider);
      // NEVER HAS A NAME(S),, EVEN WHEN NO ERROR
      var localFileName = provider.FileData.Select(multiPartData => multiPartData.LocalFileName).FirstOrDefault();

        foreach (var stream in provider.Contents)
        {
            var fileBytes = stream.ReadAsByteArrayAsync();
            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent("Successful upload", Encoding.UTF8, "text/plain");
            response.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(@"text/html");
        }
      // omitted code

Fidder标题

Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
User-Agent: Fiddler
Host: localhost:1703
Content-Length: 3199

Fiddler Body

---------------------------acebdf13572468
Content-Disposition: form-data; name="description" 
Content-Type: text

the_text_is_here
---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="today.txt"
Content-Type: text/plain

<@INCLUDE *C:\Users\loser\Desktop\today.txt*@>
---------------------------acebdf13572468-

我应该使用其他content-type吗?我在这里死了。

0 个答案:

没有答案