C# 错误:文件不是以';pdf-';asp.net
我试图在浏览器中单击链接打开PDF。它实际上是来自DB的BLOB。但我得到了一个错误:文件不是以“pdf-”asp.net开头的 这是我正在使用的代码C# 错误:文件不是以';pdf-';asp.net,c#,asp.net,C#,Asp.net,我试图在浏览器中单击链接打开PDF。它实际上是来自DB的BLOB。但我得到了一个错误:文件不是以“pdf-”asp.net开头的 这是我正在使用的代码 if (dt.Rows.Count > 0) { string strExtenstion = dt.Rows[0]["DGN0101FILE_EXTENSION"].ToString(); byte[] bytFile = (byte[])dt.Rows
if (dt.Rows.Count > 0)
{
string strExtenstion = dt.Rows[0]["DGN0101FILE_EXTENSION"].ToString();
byte[] bytFile = (byte[])dt.Rows[0]["DGN0101FILE"];
string fileName = dt.Rows[0]["DGN0101FILE_NAME"].ToString();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
if (strExtenstion == ".doc" || strExtenstion == ".docx")
{
Response.ContentType = "application/vnd.ms-word";
}
else if (strExtenstion == ".xls" || strExtenstion == ".xlsx")
{
Response.ContentType = "application/vnd.ms-excel";
}
else if (strExtenstion == ".pdf")
{
Response.ContentType = "application/pdf";
}
Response.AddHeader("Content-Length", bytFile.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(bytFile);
Response.Flush();
Response.End();
Response.Clear();
}
有效的PDF文件以
%PDF-1.5
或类似文件开头。我猜您的数据没有被正确存储或检索
- 在Acrobat/Acrobat Reader中查看原始文件,以确保它实际上是PDF
- 在文本编辑器中查看原始文件并目视检查
- 检查数据库表以确保行中有二进制数据
- 检查从数据库中提取的字节数组的长度