2009-05-28 91 views
4

我有一个下载页面,有3个下载选项:Word,Zip和PDF。有一个文件夹包含.doc文件。当用户单击页面上的Zip选项时,我希望ASP.NET将带有.doc文件的文件夹压缩到临时.zip文件中。然后客户端将从服务器下载它。当用户的下载完成时,临时Zip文件应该自行删除。如何生成临时Zip文件,然后在下载后自动删除它?

如何使用ASP.NET 2.0 C#做到这一点?

注意:我知道如何使用C#ASP.NET 2.0压缩和解压缩文件并从系统中删除文件。

回答

0

我加入这个到流码的一端固定我的问题:

Response.Flush(); 
Response.Close(); 
if(File.Exist(tempFile)) 
{File.Delete(tempFile)}; 
0

您需要手动将zip文件流式传输到用户,然后在流式传输完成后删除文件。

try 
{ 
    Response.WriteFile("path to .zip"); 
} 
finally 
{ 
    File.Delete("path to .zip"); 
} 
+0

删除 对于使用此代码在需要时类中的Response.Write行动客户端发送请求2 POST和GET到服务器。在第一次行动它的进入尝试块bla bla bla比输入finally块和删除文件..并再次发生火灾事件,并尝试下载文件,但有tempziP文件cuz它的第一个请求中删除... SOO我该如何解决这个问题? – 2009-05-28 21:37:51

7

使用DotNetZip你可以直接将zip文件保存到Response.OutputStream。不需要临时的Zip文件。

Response.Clear(); 
    // no buffering - allows large zip files to download as they are zipped 
    Response.BufferOutput = false; 
    String ReadmeText= "Dynamic content for a readme file...\n" + 
         DateTime.Now.ToString("G"); 
    string archiveName= String.Format("archive-{0}.zip", 
             DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("content-disposition", "attachment; filename=" + archiveName); 
    using (ZipFile zip = new ZipFile()) 
    { 
     // add a file entry into the zip, using content from a string 
     zip.AddFileFromString("Readme.txt", "", ReadmeText); 
     // add the set of files to the zip 
     zip.AddFiles(filesToInclude, "files"); 
     // compress and write the output to OutputStream 
     zip.Save(Response.OutputStream); 
    } 
    Response.Flush(); 
+0

你能告诉你使用ZipFile()方法的哪个dll吗? – Siddiqui 2011-10-10 07:37:12

2

表格下载从数据库,ZIP和Complate下载使用ICSharpCode.SharpZipLib.Zip

if (ds.Tables[0].Rows.Count > 0) 
      { 
       // Create the ZIP file that will be downloaded. Need to name the file something unique ... 
       string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now); 
       ZipOutputStream zipOS = new ZipOutputStream(File.Create(Server.MapPath("~/TempFile/") + strNow + ".zip")); 
       zipOS.SetLevel(5); // ranges 0 to 9 ... 0 = no compression : 9 = max compression 

       // Loop through the dataset to fill the zip file 
       foreach (DataRow dr in ds.Tables[0].Rows) 
       { 
        byte[] files = (byte[])(dr["Files"]); 
        //FileStream strim = new FileStream(Server.MapPath("~/TempFile/" + dr["FileName"]), FileMode.Create); 
        //strim.Write(files, 0, files.Length); 
        //strim.Close(); 
        //strim.Dispose(); 
        ZipEntry zipEntry = new ZipEntry(dr["FileName"].ToString()); 
        zipOS.PutNextEntry(zipEntry); 
        zipOS.Write(files, 0, files.Length); 
       } 
       zipOS.Finish(); 
       zipOS.Close(); 

       FileInfo file = new FileInfo(Server.MapPath("~/TempFile/") + strNow + ".zip"); 
       if (file.Exists) 
       { 
        Response.Clear(); 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
        Response.AddHeader("Content-Length", file.Length.ToString()); 
        Response.ContentType = "application/zip"; 
        Response.WriteFile(file.FullName); 
        Response.Flush(); 
        file.Delete(); 
        Response.End(); 
       } 
      } 
相关问题