Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:文件不是以';pdf-';asp.net_C#_Asp.net - Fatal编程技术网

C# 错误:文件不是以';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

我试图在浏览器中单击链接打开PDF。它实际上是来自DB的BLOB。但我得到了一个错误:文件不是以“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文件以
%PDF-1.5
或类似文件开头。我猜您的数据没有被正确存储或检索

  • 在Acrobat/Acrobat Reader中查看原始文件,以确保它实际上是PDF
  • 在文本编辑器中查看原始文件并目视检查
  • 检查数据库表以确保行中有二进制数据
  • 检查从数据库中提取的字节数组的长度

您从哪里得到错误信息?在代码中(如果是,在哪一行)?或者在代码执行完毕后在客户端的PDF阅读器中?@David,代码执行完毕后出现此错误。它实际上向我显示了一个打开或保存PDF的对话框。当我打开它时,会出现一个错误:文件不是以“pdf-”asp.net开头,因此听起来它不是pdf文件。如果代码完成,数据逐字节传输到客户机,那么它就按设计工作。考虑到目前为止的信息,有问题的字节流很可能不是有效的PDF文件。(也就是说,它不包含PDF阅读器用来识别有效PDF文件的文件头。)可能值得尝试使用不同的应用程序打开它,或者在记事本之类的东西中查看是否有有用的标题信息。您可以尝试将结果文件保存在光盘上,然后只是比较原始文件和结果文件的内容和长度。我猜pdf文件的内容没有正确存储到BLOB字段中。