2012-12-20 50 views
3

pdf文件我有它返回一個PDF文件與此代碼一個aspx頁面:ASP.NET - 保存在服務器上

HttpContext.Current.Response.Clear() 
HttpContext.Current.Response.ContentType = "application/pdf" 
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=Order-" & Request("OrderNo") & ".pdf") 
HttpContext.Current.Response.BinaryWrite(pdfContent) 
HttpContext.Current.Response.End() 

需要這樣的文件保存到硬盤做的代碼什麼修改在服務器上驅動?我想BinaryWrite必須用其他東西替換,但是什麼?

我非常感謝您的幫助!

回答

4

只給出路徑和pdfcontent作爲參數傳遞給File.WriteAllBytes方法像這樣:

File.WriteAllBytes(Server.MapPath("~/SubdirectoryInCurrentWebApp"),pdfContent); 

你通常無法保存任何內容您的本地應用程序目錄之外,除非您配置的應用程序下運行的應用程序池與具有足夠特權的用戶寫入目標目錄。但是開箱即用,您應該可以寫入Web應用程序中的子目錄。

+0

非常感謝,它效果很好!我只需要將文件名添加到上面的代碼中。 – user1898347

2

您可以通過調用File.WriteAllBytes將二進制數據寫入磁盤。

0

如果你只是想從pdfContent轉儲字節到一個文件,然後IO.File.WriteAllBytes("{A path}",pdfContent)應該做到這一點。