大家好我已经编写了代码来下载文件作为zip并保存,但我得到一个例外Could not find file 'C:\Program Files (x86)\IIS Express\fileName.txt'.
。这是我的代码可以帮助我一些人
protected void btnDownload_Click(object sender, EventArgs e)
{
ZipFile zip = new ZipFile();
zip.AddFile("fileName.txt");
string pathString = System.IO.Path.Combine(Path.GetTempPath(), "Attachments_" + DateTime.Now.ToString("yyyyMMddHHmmss"));
System.IO.Directory.CreateDirectory(pathString);
string sFilePath = System.IO.Path.Combine(pathString, "attachment.zip");
File.Create(sFilePath);
Response.BufferOutput = false; // for large files
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + sFilePath);
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zip.AddDirectory(pathString);
zip.Save(Response.OutputStream);
zip.Dispose();
}
答案 0 :(得分:6)
使用Server.MapPath
提供文件的绝对路径。
zip.AddFile("fileName.txt");
将成为
zip.AddFile(Server.MapPath("") + "\\fileName.txt");