Byte Serving PDFs from SQL varbinary

陌路散爱 提交于 2019-12-11 06:18:13

问题


I am want to take advantage of the web optimization for pdfs by allowing users to download them a page at a time.

The pdfs are configured for fast web view. I am serving the pdfs from sql server 2008. The c# .net 3.5 web app untilises linq to SQL to load the files into a binary array from the database. The file is opended in PDF reader plugin on the client in IE.

Any help or a shove in the right direction would be greatly appreciated.

Thanks


回答1:


If you simply want to send a PDF to the client, create an aspx file with this code:

protected void Page_Load(object sender, EventArgs e)
{
  byte[] pdfdata = GetMyPdfDataSomehow();

  Response.Clear();
  Response.ContentType = "application/pdf";
  Response.BinaryWrite(pdfdata);

  if (NoCaching)
  {
    Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
  }
  else
  {
    Response.Cache.SetExpires(DateTime.Now.AddDays(7));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
  }
  Response.End();
}

However, if you want to know how to split the PDF file up page by page, you'll need some PDF library for that.




回答2:


Returning an entire PDF is simply a case of returning the binary on the http response, as per Chris's reply.

For the page-at-a-time handling, I would suggest using something like Fiddler2 or Wireshark to look at the http traffic. There may be some custom http headers on the requests, a bit like the "resume download" support via the byte range headers.

You would need to observe a working page-by-page example, and look at what both client and server send.



来源:https://stackoverflow.com/questions/1030433/byte-serving-pdfs-from-sql-varbinary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!