-1

Iam working on a nomination portal. Users submit their nominations along with some attachments. Number of attachments may vary from 3 to 10 for each nomination. So for each nomination evaluators have to download 10 files each at a time, which is so hectic. So I thought of providing an option to download all attachments related to a nomination at once as a Zip file. I was able to download single file at once. Please provide me a solution to download multiple files at a time along with some sample code snippet. Following is the code I'm using for downloading a single file for file downloading. I am storing file content as Binary in Database

protected void DownloadFile(object sender, EventArgs e)
{
    int id = int.Parse((sender as LinkButton).CommandArgument);
    byte[] bytes;
    string fileName, contentType;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id";
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                sdr.Read();
                bytes = (byte[])sdr["Data"];
                contentType = sdr["ContentType"].ToString();
                fileName = sdr["Name"].ToString();
            }
            con.Close();
        }
    }
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = contentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush(); 
    Response.End();
}
shabareesh
  • 11
  • 2
  • You need to format your code. – artm Oct 01 '14 at 04:34
  • What is the actual question? How to create a zip file? Have you tried something already? Did you try to google that and didn't find a solution? – Panagiotis Kanavos Oct 01 '14 at 07:08
  • Hi Kanavos My actual question is How to download multiple files from database at once. Zipping is a second priority. Even if I am able to store all the files in folder then its fine. I have googled it but I didn't found an apt solution. Every solution in google is based on fetching a set of files hosted in server but not in database which is completely different from my requirement – shabareesh Oct 01 '14 at 07:17
  • possible duplicate of [How to download multiple files with one HTTP request?](http://stackoverflow.com/questions/1041542/how-to-download-multiple-files-with-one-http-request) – Panagiotis Kanavos Oct 01 '14 at 09:51
  • A simple search for "http response multiple files" yields numerous results with the same answer: You can't because HTTP wasn't designed for this. This has been answered already multiple times, eg: [How to download multiple files with one HTTP Request](http://stackoverflow.com/questions/1041542/how-to-download-multiple-files-with-one-http-request). You'll have to zip the files and send a single file to the client. – Panagiotis Kanavos Oct 01 '14 at 09:53

1 Answers1

1

You don't mention what version of .NET you are using.

With .NET 4.5, you can use the ZipFile class: ZipFile Class

An example of writing to a zip archive is shown here: ZipArchive.CreateEntry

Update:

Alternative for earlier versions of .NET is: SharpZipLib

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
  • The documentation of [ZipArchiveEntry.Open](http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchiveentry.open(v=vs.110).aspx) even has a sample on how to create an entry and write to it without first saving to disk – Panagiotis Kanavos Oct 01 '14 at 07:09
  • Hi Green, My .net version is 3.5 – shabareesh Oct 01 '14 at 07:16