Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected string fileuploader()
        {
            HttpPostedFile file1 = attach1.PostedFile;
            string outstring = null;

            if (attach1.HasFile == true)
            {
                if (file1.ContentLength < 3072000)
                {
                    string filename1 = Path.GetFileName(attach1.PostedFile.FileName);
                    string contentType1 = attach1.PostedFile.ContentType;
                    string date1 = DateTime.Now.ToString();
                    Stream fs1 = attach1.PostedFile.InputStream;

                    SqlParameter param1, param2, param3, param4;
                    SqlCommand cmd1 = new SqlCommand();

                    BinaryReader br1 = new BinaryReader(fs1);
                    byte[] bytes1 = br1.ReadBytes((int)fs1.Length);
                    cmd1.Connection = con;
                    cmd1.CommandText = "INSERT INTO [stor_attachments] VALUES (@Name, @ContentType, @Data, @DateNow)";

                    param1 = new SqlParameter();
                    param1.ParameterName = "@Name";
                    param1.Value = filename1;

                    param2 = new SqlParameter();
                    param2.ParameterName = "@ContentType";
                    param2.Value = contentType1;

                    param3 = new SqlParameter();
                    param3.ParameterName = "@Data";
                    param3.Value = bytes1;

                    param4 = new SqlParameter();
                    param4.ParameterName = "@DateNow";
                    param4.Value = date1;

                    cmd1.Parameters.Add(param1);
                    cmd1.Parameters.Add(param2);
                    cmd1.Parameters.Add(param3);
                    cmd1.Parameters.Add(param4);

                    con.Open();
                    cmd1.ExecuteNonQuery();
                    cmd1.Dispose();
                    con.Close();

                    con.Open();
                    cmd1.Connection = con;
                    cmd1.CommandText = "SELECT MAX(ID) AS 'MaxVal' FROM [stor_attachments]";
                    SqlDataReader read1 = cmd1.ExecuteReader();
                    read1.Read();
                    outstring = read1["MaxVal"].ToString();
                    cmd1.Dispose();
                    con.Close();
                }
                else
                {

                }
            }
            else
            {
                outstring = "none";
            }

            return outstring;
        }


What I have tried:

I made a program that can upload small files (3MB) to a sql database. How do I make it so that I have a single button to download all of those files into 1 rar or zip file?
Posted
Updated 11-Apr-21 21:48pm

1 solution

Your code reads the file content from the DB - you know how to do that - then ZIP (or RAR, but ZIP is built into .NET, RAR isn't) the data into a single volume: c# - Create zip file from byte[] - Stack Overflow[^]
You then send that to the client in the usual way via the Response:
C#
Response.Clear();
Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Content-Description", "File Download");
Response.AddHeader("Content-Type", "application/force-download");
Response.AddHeader("Content-Transfer-Encoding", "binary\n");
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(zipData);
Response.End();
 
Share this answer
 
Comments
[no name] 13-Apr-21 4:02am    
I know how to download the file that is stored as varbinary. I scour the internet for solutions of compiling those entries into one zip file but they fail or is not related to what I'm doing at all. Here's how i download single files.
SqlConnection con = new SqlConnection(main.ConnString);
                SqlCommand cmd1 = new SqlCommand();

                cmd1.CommandText = "SELECT * FROM [stor_attachments] WHERE [ID] = @ID";

                SqlParameter p1 = new SqlParameter();
                p1.ParameterName = "@ID";
                p1.Value = viewer1.SelectedRow.Cells[6].Text;

                cmd1.Parameters.Add(p1);

                cmd1.Connection = con;

                con.Open();

                SqlDataReader r1 = cmd1.ExecuteReader();

                r1.Read();

                bytes = (byte[])r1["stored_content"];
                contentType = r1["contentType"].ToString();
                filename = r1["filename"].ToString();
OriginalGriff 13-Apr-21 4:37am    
And you followed the link I gave to which shows how to generate a single ZIP from multiple byte arrays, yes?

So what's the problem?
[no name] 13-Apr-21 5:36am    
yeah my bad on my part.. was looking at the example code too much.. i will try to adapt it so that the Read() of a data reader is into a foreach loop then from that i will add the files 1 by 1 into the memory stream.. I will update here if my code works.. thanks ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900