2013-08-06 38 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

好吧我得到了解决方案和最简单的方法 - –

相关问题