當你說「沒有顯示」我假設你想要的客戶,而不是在服務器上打開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
你好,我不會再使用我的代碼了嗎? – KiRa
如果您想要將文件從服務器下載到客戶端,則不適用。 – VDWWD
我測試了你的代碼並且工作正常。但是它下載了PDF ..有沒有其他的'Response.Flush'方法?就像顯示它 – KiRa