【解決方法】ASP.NET とデータベースを使用してダウンロード リンクをクリックすると、pdf ファイルが開きます


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();


        }

解決策 1

ファイルが開かない場合は、ContentType とヘッダー情報が正しく設定されていない可能性があります。
次のようにしてみてください。

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();

敬具

解決策 2

あなたが使用した attachment ファイルをダウンロードするようにブラウザに指示する気質として (ファイル保存ダイアログを表示)。 使用 inline その代わり:

Response.AddHeader("Content-disposition", "inline; filename=\"" + fileName + "\"");

これはほとんどのブラウザで動作するはずですが、一部のブラウザではファイル保存ダイアログが表示される場合があります。

こちらもご覧ください コンテンツ処理 – HTTP | MDN[^].

コメント

タイトルとURLをコピーしました