【发布时间】:2014-03-20 15:42:07
【问题描述】:
您好,我有一个 pdf 生成文件,当我单击按钮时,它可以很好地下载到计算机上。但是我需要将该文件保存到我系统中的文件夹中。
我尝试了很多不同的方法,但我无法正常工作是否有任何东西可以将其保存到我在 Visual Studio 的网站中创建的 PDF 文件夹中
更新代码
Partial Class Pages_Payment
Inherits System.Web.UI.Page
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
btnPayment.Visible = False
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Receipt_" & Session("InvoNo") & ".pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
pnlPerson.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
Pages_Payment.SavePDF("~/PDF/Mypdf.pdf", pdfDoc)
End Sub
Public Shared Sub SavePDF(virtualPath As String, document As Document)
Dim msOutput As New MemoryStream()
Dim writer As New PdfWriter(document, msOutput)
Dim filebytes As Byte() = msOutput.ToArray()
Dim path As String = Server.MapPath(virtualPath)
File.WriteAllBytes(path, filebytes)
End Sub
结束类
【问题讨论】:
-
澄清一下,您想将 PDF 保存在服务器上的某个地方吗?
-
是的,所以它就像 ("~/PDF/") 我想要文件去的地方
-
如果是 iTextSharp,请参见此处的示例:stackoverflow.com/questions/12948053/save-pdf-to-disc
-
我正在尝试将其添加到我的代码中,但我知道所有那些压模、阅读器的东西意味着要知道我是否添加正确。