Click here to Skip to main content
14,450,692 members
Rate this:
Please Sign up or sign in to vote.
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:

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 20:23pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100