0

我有一个将pdf文件写入aspx页面的代码,但问题是当我想保存这个文件时,它会以aspx扩展名保存......例如:

它保存为myPDFfile.aspx,我想将其保存为myPDFfile.pdf

我必须将扩展名更改为 .pdf 才能打开它。

如何以编程方式更改扩展?

我的代码:

Dim pdfPath As String = path + Session("factura").ToString.Trim + ".pdf"
Dim client As New WebClient()
Dim buffer As [Byte]() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
4

2 回答 2

1

您应该添加Content-Disposition标题:

Dim pdfPath As String = path + Session("factura").ToString.Trim + ".pdf"
Dim client As New WebClient()
Dim buffer As [Byte]() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())

Response.AddHeader("content-disposition", "attachment; filename=myPDFfile.pdf")

Response.BinaryWrite(buffer)

您还应该阅读此问题及其答案,因为它会导致您遇到文件名字符的潜在问题。

于 2013-06-24T14:36:32.607 回答
1

您需要添加content-disposition带有文件名的标题。

 Response.AddHeader("content-disposition", "attachment; filename=myPDFfile.pdf");

这是RFC 2616第 19 节的一部分。

于 2013-06-24T14:37:34.063 回答