2015-12-24 54 views
0

我們試圖展示一個SQL Server像場爲PDF。圖像通過我們的主應用程序顯示爲PDF。我們利用另一個網絡應用程序來根據需要顯示PDF。輸出一個SQL Server圖像到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(); 
     } 
    } 
} 

編輯:問題解決見下文

回答

0
  • HttpContext.Current.Response.End()中的沖洗後,需要();

使用Chrome時發現了一個額外的問題(重複標題),雖然公司標準是IE。註釋關於目標= _blank線和附件改變下載到內嵌解決了鍍鉻重複標題的問題。

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