首页 > 解决方案 > 从数据库下载的 Pdf 文件,但我无法打开它,“加载 PDF 文档失败。” 错误

问题描述

我正在使用 MSSQL、asp.net、Visual Studio 2012。我正在编写一个代码,用于在数据库中保存/检索 pdf 文件。

我正在成功保存文件,但是当我想下载它们时,它已下载但打开文件时出错,就好像它在下载时被中断一样。但 .txt 文件已成功打开,只有 pdf 文件无法正常工作。

从数据库中检索 pdf 文件的代码在这里:

SqlConnection con = new SqlConnection(connectionstring);
con.Open();
SqlCommand com = new SqlCommand("select Name,Extn,Content from Books where Name=@Name", con);
com.Parameters.AddWithValue("Name", GridView2.SelectedRow.Cells[1].Text.ToString());
SqlDataReader dr = com.ExecuteReader();

if (dr.Read())
{
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Clear();

    Response.Buffer = true;
    Response.ContentType = dr["Extn"].ToString();
    Response.AddHeader("content-disposition", "attachment;filename=" + dr["Name"].ToString() ); // to open file prompt Box open or Save file  
   Response.TransmitFile(fi.FullName);
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite((byte[])dr["Content"]);
    Response.Flush();
    Response.Close();


Response.End(); }}

Gridview 只有文件的 Name 和 Extn,并且 Select 按钮已启用,当我选择任何 pdf(通过选择按钮)时,此方法运行(SelectedIndexChanged)。

PDF 文件已下载,但我无法打开它。

标签: c#sql-serverdatabasepdfvisual-studio-2012

解决方案


推荐阅读