0
我试图单击浏览器中的链接打开PDF。它实际上是从DB作为BLOB。但是,我得到错误:文件不具有“PDF-” asp.net错误:文件不以'pdf-'开头asp.net
这是我使用的代码开始,
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阅读器中? – David 2012-01-05 01:44:29
@David,代码执行后出现此错误。它实际上向我展示了一个打开或保存PDF的对话框。当我打开它给我的错误:文件不以'pdf-'开头asp.net – Anuya 2012-01-05 01:48:06
然后它听起来像它不是一个PDF文件。如果代码完成并且数据以字节为单位传输到客户端,那么它按照设计工作。鉴于目前的信息,所讨论的字节流很可能不是有效的PDF文件。 (也就是说,它不包含PDF阅读器用来识别有效PDF文件的文件头。)值得尝试使用不同的应用程序打开它,或使用记事本等方式检查它,以查看是否有有用的标题信息。 – David 2012-01-05 01:50:01