Posted By

housecor on 09/23/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

aunlead
Tyster


Stream file outside of web root via ashx


 / Published in: C#
 

  1. <%@ WebHandler Language="C#" Class="MyApplication" %>
  2.  
  3. using System;
  4. using System.Web;
  5. using System.IO;
  6.  
  7. public class MyApplication : IHttpHandler
  8. {
  9. public void ProcessRequest (HttpContext context)
  10. {
  11. ProfileCommon profile = (ProfileCommon)context.Profile;
  12.  
  13. if (profile.ApplicationID != default(Guid))
  14. {
  15. var filePath = string.Format("{0}{1}.pdf", SettingsManager.Forms.GetApplicationStoragePath(), profile.ApplicationID.ToString());
  16.  
  17. if (File.Exists(filePath))
  18. {
  19. context.Response.ContentType = "application/pdf";
  20. context.Response.WriteFile(filePath);
  21. context.Response.End();
  22. }
  23. }
  24. else
  25. {
  26. context.Response.ContentType = "text/plain";
  27. context.Response.Write("Could not open document.");
  28. }
  29. }
  30.  
  31. public bool IsReusable {
  32. get {
  33. return true;
  34. }
  35. }
  36.  
  37. }

Report this snippet  

You need to login to post a comment.