Download PDF

Mondeo

Centurion
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
I have a page which contains a drop down filled with forms that can be downloaded, they are all PDF's

Is there any way I can prompt the user to either save or open the file - whats happening now is the PDF just opens in the browser, I dont want this as they need to save it locally.

It would work correctly if they were .exe files instead on PDF so is there a way I can force this prompt?

Thanks
 
Content-Disposition

Rather than linking directly to the PDF, you can link to an ASP.Net page which then sends the file. This allows you to set the HTTP header required to cause a Save As prompt - Content-Disposition. Since you are serving the file from an ASP.Net page you will also need to set the other headers relating to the file and then send the file:

Visual Basic:
'dlFile is a FileInfo for the file to download:

Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & dlFile.Name)
Response.AddHeader("Content-Length", dlFile.Length.ToString())
Response.ContentType = "application/octet-stream" 'Or application/pdf, or whatever
Response.WriteFile(dlFile.FullName)
Response.End()

Good luck :cool:
 
Ah okay, so the user selects the PDF from the drop down, then in my drop downs SelectedIndexChanged event I would put

response.redirect("ServePDF.aspx?file=" + ddlPDF.SelectedValue.ToString) - which would give something like ServePDF.aspx?file=wintercheckoffer.PDF

Then on ServePDF.aspx in the page_load I would have the code you gave using the response object and getting the filename off the querysting?

What happens after response.end? Do I need to do another response.redirect back to the original page containing the dropdown?

Could I not put your code directly into the SelectedIndexChanged event and do it all from one page?

Many thanks
 
No further processing required

Could I not put your code directly into the SelectedIndexChanged event and do it all from one page?

Yes, you should be able to do that.

What happens after response.end? Do I need to do another response.redirect back to the original page containing the dropdown?

Since the downloaded file is not being displayed in the browser, the original page will remain open and useable, so you do not need to do any further processing or redirecting after serving the file. Note that this would be impossible anyway - when a file is requested, be it a web page or a binary, only one request is sent to the server and therefore only one response can be sent back - sending a file and redirecting the browser would require two responses as they are two different HTTP codes.

:)
 
Last edited:
Back
Top