Mondeo Posted January 2, 2007 Posted January 2, 2007 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 Quote
MrPaul Posted January 2, 2007 Posted January 2, 2007 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: '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: Quote Never trouble another for what you can do for yourself.
Mondeo Posted January 2, 2007 Author Posted January 2, 2007 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 Quote
MrPaul Posted January 2, 2007 Posted January 2, 2007 (edited) 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. :) Edited January 2, 2007 by MrPaul Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.