Откройте pdf-файл, когда я нажму на ссылку загрузки с помощью ASP.NET с базой данных
problem is file is downloaded but not open in new form.
Что я уже пробовал:
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(); }
suresh92
не решить