c# - 从数据库下载的 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++ - Visual Studio 2019 嵌套 Lambda 编译错误
- android - 如何在android中单击按钮时向JSONObject添加值?
- json - 将 JSON 数据解析为 ORA-40441 时出错:JSON 语法错误
- c# - 如何将 ProcessStartInfo 写入 Linux 的文本文件
- php - 学说获得销量最多的产品
- sql - MongoDB超时查询
- algorithm - 删除左倾红黑树及其不变量
- c# - WebRoot 和 ContentRoot 用于更改 ASP.NET Core 应用程序的“Web 部件”位置的目的?
- swift - 带有日期选择器工具栏的 iOS 14 错误
- javascript - 在 Next JS 中访问 JSON 对象中的数据