Reputation: 8636
Hi all I have written the code to download the file as zip and save but I am getting an exception as Could not find file 'C:\Program Files (x86)\IIS Express\fileName.txt'.
. This is my code can some one help me
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();
}
Upvotes: 2
Views: 692
Reputation: 1796
use Server.MapPath
to provide absolute path to the file.
zip.AddFile("fileName.txt");
will become
zip.AddFile(Server.MapPath("") + "\\fileName.txt");
Upvotes: 6