图标zip无法将文件保存在目标路径中

时间:2014-05-15 13:12:10

标签: c# asp.net zipfile

大家好我已经编写了代码来下载文件作为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();
    }

1 个答案:

答案 0 :(得分:6)

使用Server.MapPath提供文件的绝对路径。

zip.AddFile("fileName.txt");

将成为

zip.AddFile(Server.MapPath("") + "\\fileName.txt");