从Htt的prequestMessage下载zip文件:控制器 [英] Downloading zip file from HttpRequestMessage : controller
本文介绍了从Htt的prequestMessage下载zip文件:控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我工作的一个Web API的使用ASP.NET MVC的其中的下载在 zip文件附加在的Htt prequestMessage
I'm working on a web api's using ASP.NET MVC's which download the zip file attached in the HttpRequestMessage
如下
var task = this.Request.Content.ReadAsStreamAsync();
task.Wait();
if (this.Request.Content.IsMimeMultipartContent())
{
using (Stream requestStream = task.Result)
{
// Do not know how to copy the above request to file stream or zip (ionic zip) and generate zip file from it
}
}
请注意:
1)
using (var fileStream = File.Create("name.zip"))
{
requestStream.CopyTo(fileStream);
}
创建无效的拉链。
creates invalid zip..
2)邮编里面包含了很多的文件。
2) Zip contains many files inside it.
等待您的意见
更新1:
var provider = new MultipartFormDataStreamProvider(ScriptPath);
Request.Content.ReadAsMultipartAsync(provider);
foreach (MultipartFileData file in provider.FileData)
{
Trace.Write(file.Headers.ContentDisposition.FileName);
Trace.Write("Server file path: " + ScriptPath);
}
虽然并不能帮助我
though does not help me
推荐答案
这对我的作品。请把伪因为我没有编译。 <击>你也可能要返工阻塞。结果
呼吁采取异步的优势阅读。击>
This works for me. Please treat as pseudo as I didn't compile it. Also you may want to rework the blocking .Result
call to take advantage of the Async read.
var requestStream = await Request.Content.ReadAsMultipartAsync();
HttpContent fileContent = requestStream.Contents.SingleOrDefault(c => c.Headers.ContentType != null);
byte[] fileBytes = await fileContent.ReadAsByteArrayAsync();
using (FileStream fs = File.OpenWrite(outputPath))
{
fs.Write(fileBytes, 0, (int)fileContent.Headers.ContentLength.Value);
}
这篇关于从Htt的prequestMessage下载zip文件:控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文