Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
problem is file is downloaded but not open in new form.


What I have tried:

protected void DownloadFile(object sender, EventArgs e)
        {
          //  string filePath = (sender as LinkButton).CommandArgument;
            int id = int.Parse((sender as LinkButton).CommandArgument);
            byte[] bytes;
            string fileName, contentType;
            string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select Name,FileName,ContentType,Data 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 = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);

            //Response.ContentType = ContentType;
            //Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            //Response.WriteFile(filePath);
            //Response.End();

            Response.BinaryWrite(bytes);            
            Response.End();

        //{
        //    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        //    LinkButton lnkbtn = sender as LinkButton;
        //    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
        //    int fileid = Convert.ToInt32(GrvImageFiles.DataKeys[gvrow.RowIndex].Value.ToString());
        //  //  string name, type;
        //    using (SqlConnection con = new SqlConnection(strConnString))
        //    {
        //        using (SqlCommand cmd = new SqlCommand())
        //        {
        //            cmd.CommandText = "select Name,FileName, ContentType,Data from tblFiles where Id=@Id";
        //            cmd.Parameters.AddWithValue("@id", fileid);
        //            cmd.Connection = con;
        //            con.Open();
        //            SqlDataReader dr = cmd.ExecuteReader();
        //            if (dr.Read())
        //            {
        //                Response.ContentType = dr["FileType"].ToString();
        //                Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["FileName"] + "\"");
        //                Response.BinaryWrite((byte[])dr["FileData"]);
        //                Response.End();
        //            }
        //        }
        //    }

     //2.     //string filePath = (sender as LinkButton).CommandArgument;
            //Response.ContentType = ContentType;
            //Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            //Response.WriteFile(filePath);
            //Response.End();


        }
Posted
Updated 22-Nov-17 22:57pm
Comments
suresh92 22-Nov-17 23:55pm    
not solved

If the file is not opening then the issue is likely that the ContentType & Header information is not being correctly set.
Try as follows;
C#
// NOTE the additional back slashes are to escape the quotes which are actually required
Response.ContentType = "\".pdf\", \"application/pdf\"";
Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.End();


Kind Regards
 
Share this answer
 
You have used attachment as disposition which tells the browser to download the file (show a file saving dialog). Use inline instead:
Response.AddHeader("Content-disposition", "inline; filename=\"" + fileName + "\"");

This should work with most browsers but some might still show a file saving dialog.

See also Content-Disposition - HTTP | MDN[^].
 
Share this answer
 
Comments
suresh92 27-Nov-17 2:37am    
ok Jochen Arndt but i need this in browser popup.
Jochen Arndt 27-Nov-17 2:52am    
So you want the browser to show a popup asking what to do with the file like
"Open in new frame, open with application, or save as?"

You can't control that from the server side because it depends on the browser and how he is configured.
suresh92 27-Nov-17 4:22am    
ok Jochen Arndt,u give answer.it's open in tab but i need to show pdf in google chrome browser like popup.
8985216241 watsapp number
suresh92 29-Nov-17 6:50am    
hi Jochen Arndt,u give answer.it's open in tab but i need to show pdf in google chrome browser like popup
Jochen Arndt 29-Nov-17 7:30am    
What do you mean by popup? Open in a PDF viewer?

There are usually three or four options for the browser:
- It shows a "Save as" dialog
- It opens the PDF in the browser
- It starts an external PDF viewer
- It asks what to do offering all or only two of the previous options

But you can't define the browser's behaviour from the server side!
It depends on the browser (Chrome, IE, Firefox, Safari, ...) and how that is configured by the user.

All you can do is giving the browser a hint with the disposition "inline" or "attachment".

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