简体   繁体   English

如何从Web API 2 HttpGet发送zip文件

[英]How to send a zip file from Web API 2 HttpGet

I'm trying to figure it out how to create new zip file from a given folder path, and sending it back to the sender. 我试图弄清楚如何从给定的文件夹路径创建新的zip文件,并将其发送回发件人。

The need is that the file will be downloaded at the sender that requested it. 需要的是文件将在请求该文件的发件人处下载。 I've seen alot of answers but none helped me with the exact answer. 我看到了很多答案,但没有一个能帮助我找到确切的答案。

My code: 我的代码:

Guid folderGuid = Guid.NewGuid(); Guid folderGuid = Guid.NewGuid(); string folderToZip = ConfigurationManager.AppSettings["folderToZip"] + folderGuid.ToString(); 字符串folderToZip = ConfigurationManager.AppSettings [“ folderToZip”] + folderGuid.ToString();

Directory.CreateDirectory(folderToZip); Directory.CreateDirectory(folderToZip);

string directoryPath = ConfigurationManager.AppSettings["DirectoryPath"]; 字符串directoryPath = ConfigurationManager.AppSettings [“ DirectoryPath”]; string combinedPath = Path.Combine(directoryPath, id); 字符串CombinedPath = Path.Combine(directoryPath,id);

DirectoryInfo di = new DirectoryInfo(combinedPath); DirectoryInfo di =新的DirectoryInfo(combinedPath); if (di.Exists) { //provide the path and name for the zip file to create string zipFile = folderToZip + "\\" + folderGuid + ".zip"; if(di.Exists){//提供zip文件的路径和名称以创建字符串zipFile = folderToZip +“ \\” + folderGuid +“ .zip”;

//call the ZipFile.CreateFromDirectory() method
ZipFile.CreateFromDirectory(combinedPath, zipFile, CompressionLevel.Fastest, true);

var result = new HttpResponseMessage(HttpStatusCode.OK);
using (ZipArchive zip = ZipFile.Open(zipFile, ZipArchiveMode.Read))
{
    zip.CreateEntryFromFile(folderFiles, "file.zip");
}

var stream = new FileStream(zipFile, FileMode.Open);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
    FileName = "file.zip"
};
log.Debug("END ExportFiles()");
return ResponseMessage(result);

In your controller: 在您的控制器中:

using System.IO.Compression.FileSystem; // Reference System.IO.Compression.FileSystem.dll

[HttpGet]
[Route("api/myzipfile"]
public dynamic DownloadZip([FromUri]string dirPath)
{
if(!System.IO.Directory.Exists(dirPath))
   return this.NotFound();

    var tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid()); // might want to clean this up if there are a lot of downloads
    ZipFile.CreateFromDirectory(dirPath, tempFile);
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StreamContent(new FileStream(tempFile, FileMode.Open, FileAccess.Read));
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = fileName;
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");

  return response;
}

UPD: Workaround the File Already Exists UPD:解决文件已存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM