【问题标题】:Adobe PDF not showing on IIS7Adobe PDF 未在 IIS7 上显示
【发布时间】:2017-09-02 09:30:26
【问题描述】:

我得到下面的代码在本地 (using source code) 上工作得很好。但是当我在IIS7 上发布它时,PDF 不再显示了.. IIS 是否有问题? . .我花了很多天来解决这个问题。

Dim strPath = Server.MapPath("~\Reports\GeneratedReport.pdf")

my_rpt.ExportToDisk(ExportFormatType.PortableDocFormat, strPath)
Dim file = New System.IO.FileInfo(strPath)
Dim Process = New Process()
If file.Exists Then
    Process.StartInfo.UseShellExecute = True
    Process.StartInfo.FileName = strPath
    Process.Start()
Else
    'No Report found
End If

您可以在下图中看到 Adob​​eReader 正在运行,但它没有显示在我的屏幕上。

【问题讨论】:

    标签: asp.net pdf iis


    【解决方案1】:

    如果您想在您的网站上显示 PDF,可以使用以下一些 javascript 工具来帮助您完成此操作:

    http://viewerjs.org/

    https://github.com/mozilla/pdf.js/

    就我个人而言,我没有使用过这两种工具,但它们似乎足以完成您想要完成的任务。

    【讨论】:

      【解决方案2】:

      当您说“不显示”时,我假设您希望在客户端而不是服务器上打开 PDF。通常,您会将文件发送到浏览器。 Process.Start()会在服务器端启动一个进程,所以即使允许AppPool启动一个进程,它也只会打开服务器上的pdf。 下面是从服务器向客户端发送文件的方法。

      string strPath = Server.MapPath("~/reports/GeneratedReport.pdf");
      
      //read the file from disk and convert to a byte array
      byte[] bin = File.ReadAllBytes(strPath);
      
      //clear the buffer stream
      Response.ClearHeaders();
      Response.Clear();
      Response.Buffer = true;
      
      //set the correct contenttype
      Response.ContentType = "application/pdf";
      
      //set the correct length of the data being send
      Response.AddHeader("content-length", bin.Length.ToString());
      
      //set the filename for the file
      Response.AddHeader("content-disposition", "attachment; filename=\"GeneratedReport.pdf\"");
      
      //send the byte array to the browser
      Response.OutputStream.Write(bin, 0, bin.Length);
      
      //cleanup
      Response.Flush();
      HttpContext.Current.ApplicationInstance.CompleteRequest();
      

      VB

      Dim strPath As String = Server.MapPath("~/reports/GeneratedReport.pdf")
      
      'read the file from disk and convert to a byte array
      Dim bin() As Byte = File.ReadAllBytes(strPath)
      
      'clear the buffer stream
      Response.ClearHeaders
      Response.Clear
      Response.Buffer = true
      
      'set the correct contenttype
      Response.ContentType = "application/pdf"
      
      'set the correct length of the data being send
      Response.AddHeader("content-length", bin.Length.ToString)
      
      'set the filename for the file
      Response.AddHeader("content-disposition", "attachment; filename=""GeneratedReport.pdf"""")", send the byte array to the browser, Response.OutputStream.Write(bin, 0, bin.Length))
      
      'cleanup
      Response.Flush
      HttpContext.Current.ApplicationInstance.CompleteRequest
      

      【讨论】:

      • 如果您想将文件从服务器下载到客户端,则不需要。
      • 我测试了你的代码并且工作正常.. 但是它下载了 PDF.. 有没有 Response.Flush 的替代方法?就像显示它一样
      • 刷新与 PDF 的客户端处理无关。您无法控制文件发送后会发生什么。从那时起,客户就处于控制之中。根据浏览器类型和用户偏好,打开、保存到磁盘等。
      • 我只是在它传入刷新它下载 PDF 时注意到它。这就是为什么我问是否有没有下载它的替代方式?只需像打开普通 PDF 文件一样在屏幕上显示即可。
      • 不,在屏幕上显示它是由客户端上的浏览器/pdf阅读器完成的。你不能强迫它。例如,我的设置总是询问下载文件时会发生什么,而不是直接打开它们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 2017-02-19
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多