Kentico - Force download of PDF


/ Published in: ASP
Save to your folder(s)

"This was a requirement that the client had because there were differences in how various browsers were displaying the same PDFs. By setting the "Content-Disposition" header to "attachment" it forces the user to download the file. I thought this was kind of cool, so I thought I'd share it. This is how I implemented it for Kentico's GetFile handler."


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Sends the given file within response
  3. /// </summary>
  4. /// <param name="file">File to send</param>
  5. protected void SendFile(CMSOutputFile file)
  6. {
  7. // Clear response.
  8. Response.Cookies.Clear();
  9. Response.Clear();
  10.  
  11. Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
  12.  
  13. if ((file != null) && file.IsValid)
  14. {
  15. if (file.MimType == "application/pdf")
  16. {
  17. // file is a PDF -- force it to download ("save as")
  18. Response.AddHeader("Content-Disposition", "attachment; filename" + file.Attachment.AttachmentName);
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.