【问题标题】:Output of a SQL Server Image to PDF将 SQL Server 图像输出为 PDF
【发布时间】:2016-01-06 04:50:20
【问题描述】:

我们尝试将 SQL Server 图像字段显示为 PDF。图像通过我们的主应用程序显示为 PDF。我们还根据需要使用另一个 Web 应用程序来显示 PDF。

多年来,这一直有效,但是最近有一些(但不是全部)扫描的 PDF 文档无法在 Web 应用程序中正确显示,即使它们显示在主应用程序中也是如此。此错误似乎来自新的扫描仪。收到的消息是:“文件已损坏,无法修复。”下面是用于呈现 PDF 的代码。任何建议将不胜感激。

public partial class main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["OurConnectionString"].ToString();
        string sql = "our_stored_procedure_PDF";
        object image = SqlHelper.ExecuteScalar(strConn,CommandType.StoredProcedure,sql,new SqlParameter("@recordid",Request.QueryString["idnum"].ToString()),new SqlParameter("@loc",Request.QueryString["locationID"].ToString()));
        byte[] pdf = null;
        if (image != null && image != DBNull.Value)
        {
            pdf = (byte[])image;
        }
        if (pdf != null)
        {
            long FileSize = pdf.Length;

            long StartPos = 0, EndPos;

            EndPos = pdf.Length;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();

            String type = "Application/pdf";

            if (String.Empty != type)
            {
                HttpContext.Current.Response.ContentType = type;
            }

            String Range = HttpContext.Current.Request.Headers["Range"];

            if (null != Range && String.Empty != Range)
            {
                String[] StartEnd = (Range.Substring(Range.LastIndexOf("=") + 1).Split('-'));

                if (StartEnd[0] != null)
                {
                    StartPos = long.Parse(StartEnd[0]);
                }

                if (StartEnd.GetUpperBound(0) >= 1 && null != StartEnd[1])
                {
                    EndPos = long.Parse(StartEnd[1]);
                }
                else
                {
                    EndPos = FileSize - StartPos;
                }

                if (EndPos > FileSize)
                {

                    EndPos = FileSize - StartPos;

                }

                HttpContext.Current.Response.StatusCode = 206;
                HttpContext.Current.Response.StatusDescription = "Partial Content";
                HttpContext.Current.Response.AppendHeader("Content-Range", "bytes " + StartPos.ToString() + "-" + EndPos.ToString() + "/" + FileSize.ToString());
            }

            HttpContext.Current.Response.AppendHeader("Content-disposition", "inline; target=" + "_blank");

            if ((type != String.Empty) && (StartPos != 0))
            {
                HttpContext.Current.Response.ContentType = type;
            }
            bool forceDownload = true;
            if (forceDownload) // ' Will cause a download dialog (not using this method for the PDFs)
            {
                HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=Order.pdf");
            }

            HttpContext.Current.Response.OutputStream.Write(pdf, (int)StartPos, (int)(StartPos + EndPos));
            HttpContext.Current.Response.Flush();
        }   
    }
}

编辑:问题已解决,见下文

【问题讨论】:

    标签: html sql sql-server pdf


    【解决方案1】:
    • 在 Flush() 之后需要 HttpContext.Current.Response.End();

    虽然公司标准是 IE,但在使用 Chrome 时发现了另一个问题(重复标题)。注释掉 target=_blank 的行并将下载从附件更改为内联解决了 Chrome 重复标头问题。

    HttpContext.Current.Response.AppendHeader("Content-disposition", "inline; filename=Order.pdf");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多