我正在使用ASP.NET MVC开发一个web api,下载 HttpRequestMessage
中附带的 zip文件如下
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);
}
创建无效的zip ..
2)Zip里面包含很多文件。
等待你的意见
更新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);
}
虽然没有帮助我
答案 0 :(得分:0)
这对我有用。请视为伪,因为我没有编译它。 此外,您可能需要重新设置阻止 .Result
调用以利用异步读取。
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);
}