Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to download my file in dynamically created directory. and want to download files in different different folders with the name of version .Is there possible to download multiple files in different different folders at the selection of checkbox .
first priority to download data in folders

What I have tried:

C#
foreach (GridViewRow row in grdCustomer.Rows)
            {

                var isChecked = (row.FindControl("CheckBox1") as CheckBox);
                if (isChecked.Checked)
                {
                    string filename = (row.FindControl("lblFileName") as HyperLink).Text;




                    byte[] bytes;

                    string fileName, contentType, version;
                    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.CommandText = "select uploadfilename,filename, Description, version,ContentType from upload_test where filename=@filename";
                            cmd.Parameters.AddWithValue("@filename", filename);
                            cmd.Connection = con;
                            con.Open();
                            using (SqlDataReader sdr = cmd.ExecuteReader())
                            {
                                sdr.Read();
                                bytes = (byte[])sdr["Description"];
                                contentType = sdr["ContentType"].ToString();
                                fileName = sdr["uploadfilename"].ToString();
                                version = sdr["version"].ToString();

                                // string dateString = DateTime.Now.ToString("");



                            }
                        }




                        //string path = HttpContext.Current.Server.MapPath("~/" + version + "/");
                        //Directory.CreateDirectory(path);
                        ////FileAttributes att = File.GetAttributes(path);
                        ////File.SetAttributes(path,att);

                        //DirectorySecurity sec = Directory.GetAccessControl(path);
                        //var appDataPath = Server.MapPath("~/db/");

                        //if (!Directory.Exists(appDataPath))
                        //{
                        //    Directory.CreateDirectory(appDataPath);
                        //}



                        Response.Clear();
                            Response.Buffer = true;

                            Response.Charset = "";
                            Response.Cache.SetCacheability(HttpCacheability.NoCache);
                            Response.ContentType = contentType;

                            Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
                            Response.BinaryWrite(bytes);
                        //string id = path + filename + contentType;

                        Response.TransmitFile(HttpContext.Current.Server.MapPath("~/db/"));


                        //Response.WriteFile(path);
                        Response.End();
Posted
Updated 21-Jun-19 19:23pm
v2

1 solution

Look at your code:
Response.TransmitFile(HttpContext.Current.Server.MapPath("~/db/"));
Does not specify a file: it specifies a folder "db" which resides in the main site folder.

You can't "send a directory", just a file. Use Path.Combine[^] to add the actual filename to the end of that before sending ...

And in future do us all a favour. Don't just "dump" your code on us: format it using the pre tags via the code widget above the text box so that the formatting is preserved and the syntax highlighter engaged. And delete commented out code as well - it's of no use to us!
As it is, you need to re-indent that correctly (CTRL + K, D will do it) and then you might see that you send the bytes and the file together anyway.
 
Share this answer
 

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