ASP.NET - 在服务器上保存 pdf 文件

use*_*347 3 vb.net asp.net

我有一个 aspx 页面,它返回一个带有以下代码的 pdf 文件:

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()
Run Code Online (Sandbox Code Playgroud)

需要对代码做哪些修改才能将文件保存到服务器的硬盘上?我猜 BinaryWrite 必须用其他东西替换,但是什么?

我非常感谢您的帮助!

Ica*_*rus 5

只需指定 Path 和 pdfcontent 作为该File.WriteAllBytes方法的参数,如下所示:

File.WriteAllBytes(Server.MapPath("~/SubdirectoryInCurrentWebApp"),pdfContent);
Run Code Online (Sandbox Code Playgroud)

您通常无法在本地应用程序目录之外保存任何内容,除非您使用具有足够权限写入目标目录的用户配置应用程序运行所在的应用程序池。但是开箱即用,您应该能够写入 Web 应用程序内的子目录。