2013-08-06 87 views
-1

我必須以zip格式下載pdf和doc類型的多個文件。 文件以二進制格式存儲,其擴展名也保存在數據庫中。 請描述如何以zip格式下載這些文件。 我爲此使用了離子拉鍊。我試過下面的代碼,但它不工作 -如何以zip格式下載以二進制格式存儲在數據庫中的文件

protected void ZipDownload() 
{ 
    var list = db.Documents.Where(u => u.userId == (int)Session["usrId"]).Select(u => new { u.doc, u.docname, u.doctype }); 
    ZipFile zip = new ZipFile(); 
    foreach (var file in list) 
    { 
     zip.AddEntry(file.docname,(byte[])file.doc.ToArray()); 
    } 
    var zipMs=new MemoryStream(); 
    zip.Save(zipMs); 

    zipMs.Seek(0, SeekOrigin.Begin); 

    zipMs.Flush(); 
} 
+4

你的問題究竟是什麼,你可以展示一些代碼,你有嘗試過什麼嗎? – x4rf41

+0

zip文件是二進制文件... – ganders

+0

另外,作爲一個附註,不要將文件本身存儲在數據庫中,這將導致非常非常臃腫的數據庫。只將文件名存儲在數據庫中,並將文件存儲在某個文件夾中。 – bizzehdee

回答

0

下載多個文件以zip格式使用這個 -

protected void ZipDownload() 
    { 
     var list = db.Documents.Where(u => u.userId == (int)Session["usrId"]).Select(u => new { u.doc, u.docname, u.doctype }); 
     ZipFile zip = new ZipFile(); 
     foreach (var file in list) 
     { 

      zip.AddEntry(file.docname, (byte[])file.doc.ToArray()); 
     } 
     var zipMs = new MemoryStream(); 
     zip.Save(zipMs); 
     byte[] fileData = zipMs.GetBuffer(); 
     zipMs.Seek(0, SeekOrigin.Begin); 
     zipMs.Flush(); 
     Response.Clear(); 
     Response.AddHeader("content-disposition", "attachment;filename=docs.zip "); 
     Response.ContentType = "application/zip"; 
     Response.BinaryWrite(fileData); 
     Response.End(); 

    } 

這個代碼是完美的工作。

0

這裏是我目前用來將文件添加到WinZip存檔的方法。您可以遍歷文件列表並將每個文件傳遞給此方法。我確定有一種方法可以一次添加文件列表,或者優化我的方法。

/// <summary> 
/// Add files to a WinZip archive. 
/// </summary> 
/// <remarks> 
/// This uses System.IO.Compression 
/// </remarks> 
/// <param name="file_to_archive">Fully-qualified name of file being added to the archive.</param> 
/// <param name="archive_file">Fully-qualified name of the archive.</param> 
/// <returns>TRUE on success</returns> 
public bool AddFileToArchive(string file_to_archive, string archive_file) 
{ 
    bool retcode = true; 

    //First make sure the archive and the file exist 


    //Add the file to the archive 
    try 
    { 
     using (FileStream zipToOpen = new FileStream(archive_file, FileMode.Open)) 
     { 
      using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) 
      { 
       ZipArchiveEntry readmeEntry = archive.CreateEntry(file_to_archive); 
      } 
      zipToOpen.Close(); 
     } 
    } 
    catch (System.IO.FileNotFoundException fe) 
    { 
     //Create the WZ file and try again 
     //If creating the file does not work, do not try again (preventing an infinite recursion) 
     FileInfo archiveInfo = new FileInfo(archive_file); 
     if (archiveInfo.Exists == false) 
     { 
      archiveInfo.Create(); 
      if (archiveInfo.Exists == true) 
      { 
       retcode = AddFileToArchive(file_to_archive, archive_file); 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     retcode = false; 
    } 

    return (retcode); 

} 
+0

好吧我得到了解決方案和最簡單的方法 - –

相關問題