0

Im trying to search and download the files uploaded to the database,I can able to retrieve the files from the database. Downloading single file works but cant download multiple files at a time,I have read about downloading as a zip file,Can anyone help??????

     using (sampledbase dbcontext = new sampledbase ())
       {

           ZipFile zip = new ZipFile();
           long id = Convert.ToInt64(Request.QueryString["id"]);
           System.Nullable<long> fileid = id;
           var query = dbcontext.searchfile(ref fileid );

          foreach (var i in query)
           {
               byte[] binarydata = i.Data.ToArray();
               Response.AddHeader("content-disposition", string.Format("attachment; filename =\"{0}\"", i.Name));
               Response.BinaryWrite(binarydata);
               Response.ContentType = i.ContentType;
               Response.End();
           }
       }

1 Answers1

0

I see that you are using ZipFile so you are in the right direction. In order to zip your files, you can save your files in a directory and zip the directory using this code :

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);

In your case, look at the data type returned by dbcontext.searchfile() and save the content to a directory using something like StreamWriter. Looking at your code, the type seems to be a byte[], you can see this link to learn how to save the bytes in a file : Write bytes to file

Another solution is to zip directly the bytes[] : Create zip file from byte[]

Community
  • 1
  • 1
JP Tétreault
  • 600
  • 5
  • 15