Adobe PDF not showing on IIS7

前端 未结 2 1678
小鲜肉
小鲜肉 2021-01-23 17:04

I got this code below working on local (using source code) perfectly fine. But when I published it on IIS7 the PDF is not showing anymore.

2条回答
  •  迷失自我
    2021-01-23 17:38

    When you say "not showing" I am assuming you want the PDF to be opened on the client, not the server. Normally you would send the file to the browser. Process.Start() will start a process server-side, so even if the AppPool is allowed to start a process, it will only open the pdf on the server. Below is how you send a file from the server to the client.

    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
    

提交回复
热议问题