首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用asp.net下载Zip文件

无法使用asp.net下载Zip文件
EN

Stack Overflow用户
提问于 2017-07-27 21:48:54
回答 2查看 1.4K关注 0票数 2

我有一个文件夹,里面有3-4 pdf文件。所以在按钮点击,我想下载的ZIP文件的文件。为此,我编写了以下代码

代码语言:javascript
复制
string strFilePath = HttpContext.Current.Server.MapPath("~/UploadedFiles/" + SAP_ID + '_' + CANDIDATEID + "\\" + SAP_ID + '_' + CANDIDATEID + ".pdf");
string strDirectory = HttpContext.Current.Server.MapPath("~/UploadedFiles/" + SAP_ID + '_' + CANDIDATEID);

HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

if (Directory.Exists(strDirectory))
{
    if (File.Exists(strFilePath + ".zip"))
    {
        var filestream = new System.IO.FileStream(strFilePath + ".zip", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
        filestream.Close();
        File.Delete(strFilePath + ".zip");
     }                   
}

//  ZipFile.CreateFromDirectory(strDirectory, strDirectory + ".zip");

var stream = new FileStream(strDirectory + ".zip", FileMode.Open);

result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(strDirectory + ".zip");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;

但是点击一下按钮,ZIP就不会下载,浏览器就会加载。

请告诉我原因是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-27 22:26:18

您可能会更幸运地使用Response.TransmitFile方法,下面是一个使用压缩文件地址的示例-

代码语言:javascript
复制
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=file.zip");
Response.TransmitFile(strFilePath + ".zip");
Response.Flush();
票数 1
EN

Stack Overflow用户

发布于 2017-07-27 22:10:51

如果没有创建压缩文件,那么您可能需要向IIS AppPool\ your AppPool授予对UploadedFiles目录的写/修改权限。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45352757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档