基本上我想将文件流式传输到Web API,一旦进入web api控制器,我想通过流式读取器传递数据,因为它会进入低级逻辑.我尝试了从另一个SO帖子中找到的以下代码进行了一些修改,但我得到:
An asynchronous module or handler completed while an asynchronous
operation was still pending.
public async void Put(int id,HttpRequestMessage request) { if (!Request.Content.IsMimeMultipartContent()) throw new InvalidOperationException(); var provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider); var file = provider.Contents.First(); var filename = file.Headers.ContentDisposition.FileName.Trim('\"'); var buffer = await file.ReadAsByteArrayAsync(); var stream = new MemoryStream(buffer); using (var s = new StreamReader(stream)) { saveFile.Execute(id,s); } }
解决方法
将async void更改为async Task