【问题标题】:Retrieve PDF file stored in SQL Server database and then display on page检索存储在 SQL Server 数据库中的 PDF 文件,然后在页面上显示
【发布时间】:2013-12-24 07:51:39
【问题描述】:

我正在开发 asp.net 应用程序,它在页面上显示从数据库中检索到的 pdf 文件。我遇到了 pdf 文件未在页面中显示的问题

我附上我尝试过的代码:

        con.Open();

        SqlCommand cmd = new SqlCommand("select Pdf from SavePdf where IC='" + id +   
        "'", con);
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            byte[] fileData = (byte[])dr.GetValue(0);

            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(fileData);
            Response.Flush();
            Response.End();                   
        }
        dr.Close(); 
       }

【问题讨论】:

  • 我尝试了您的代码,它对我来说非常适合。您是否遇到了某种错误或什么?
  • @Ramashankar,我得到了显示连接的空白页面。
  • 您是否验证了您从数据库中获取的 PDF 数据/字节?
  • @Ramashankar,是的。当我尝试保存到本地机器时,我已经验证了它,它工作正常。
  • 你可以试试我刚刚发布的代码here

标签: asp.net sql sql-server pdf


【解决方案1】:

使用此代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = int.Parse(Request.QueryString["id"]);
            Display(id);
        }
    }

    private void Display(int id)
    {
        DataSet ds = GetData("select * from tblFiles where Id=@Id", id);
        Byte[] bytes = (Byte[])ds.Tables[0].Rows[0]["Data"];
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }

    private DataSet GetData(string query, int id)
    {
        string conString = ConfigurationManager.ConnectionStrings["constr_files"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                cmd.Parameters.Add("@Id", id);
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
    }

【讨论】:

    【解决方案2】:

    我尝试了您的代码,它非常适合我。

    protected void ShowPdfButton_Click(object sender, EventArgs e)
        {
    
            byte[] fileData = GetPdfBytes(); 
    
            Response.Buffer = true;
            Response.Charset = "";
            Response.Clear();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(fileData);
            Response.Flush();
            Response.End();  
        }
    
        private byte[] GetPdfBytes()
        {
            string pdfFileFullName = @"C:\Reports\Test.pdf";
            //TODO: you can fetch the bytes from database as well and return. i have used pdf file.
            return System.IO.File.ReadAllBytes(pdfFileFullName);
        }
    

    【讨论】:

    • 我设法解决了需要将 response.clear 放在 response.contentype 之前的问题。
    猜你喜欢
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多