我有一个文件夹,其中有3-4 pdf
个文件。点击按钮,我想将PDF文件作为ZIP
文件下载。为此我写了下面的代码
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没有下载,浏览器只是加载。
请帮助我知道原因是什么?
答案 0 :(得分:1)
您可以通过Response.TransmitFile方法获得更多好运,下面是使用您的zip文件地址的示例 -
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=file.zip");
Response.TransmitFile(strFilePath + ".zip");
Response.Flush();
答案 1 :(得分:0)
如果没有创建zip文件,您可能需要将UploadedFiles目录的写入/修改权限授予IIS AppPool \ yourAppPoolName。