在Response.ContentType =“application / pdf”中更改pdf扩展名的aspx扩展名

时间:2013-06-24 14:34:22

标签: asp.net pdf

我有一个代码将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)

2 个答案:

答案 0 :(得分: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)

您还应该阅读this question and its answer,因为它会引发您对文件名字符的潜在问题。

答案 1 :(得分:1)

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

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

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