Renato Wong
Renato Wong

Reputation: 1

change aspx extension for pdf extension in Response.ContentType = "application/pdf"

I have a code that writes a pdf file into an aspx page, but the problem is that when I want to save this file it saves with aspx extension... for example:

It saves as myPDFfile.aspx and I want to save it as myPDFfile.pdf

and I have to change the extension to .pdf to be able to open it.

How can I change the extension programatically?

My code:

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)

Upvotes: 0

Views: 4970

Answers (2)

Oded
Oded

Reputation: 499002

You need to add a content-disposition header with the file name.

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

This is part of RFC 2616, section 19.

Upvotes: 1

Steve B
Steve B

Reputation: 37660

You should add the Content-Disposition header :

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)

You should also read this question and its answer as it will leads you to potential issue with filename characters.

Upvotes: 1

Related Questions