andrew4582

File Downloader ASP.NET

Aug 15th, 2010
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5.  
  6. using System.Data;
  7. using System.Data.Sql;
  8. using System.Data.SqlClient;
  9. using System.Data.OleDb;
  10. using System.Data.Common;
  11. using System.Data.SqlTypes;
  12. using System.Data.ProviderBase;
  13.  
  14. using System.Text;
  15. using System.Threading;
  16. using System.IO;
  17. using System.Net;
  18. using System.Diagnostics;
  19.  
  20. using System.Xml;
  21. using System.Xml.Serialization;
  22. using System.Xml.Schema;
  23. using System.Xml.XPath;
  24. using System.Xml.Xsl;
  25.  
  26. using System.Configuration;
  27.  
  28. using System.Globalization;
  29. using System.Reflection;
  30. using System.Data.SqlClient;
  31. using System.Data.Common;
  32. using System.Drawing;
  33.  
  34. using System.Web;
  35. using System.Web.Util;
  36. using System.Web.UI;
  37. using System.Web.SessionState;
  38. using System.Web.Security;
  39. using System.Web.Profile;
  40. using System.Web.Mail;
  41. using System.Web.Handlers;
  42. using System.Web.Hosting;
  43. using System.Web.DataAccess;
  44. using System.Web.Configuration;
  45. using System.Web.Compilation;
  46. using System.Web.Caching;
  47. using System.Web.Administration;
  48. using System.Web.Services;
  49. using System.Web.Services.Configuration;
  50. using System.Web.Services.Description;
  51. using System.Web.Services.Protocols;
  52. using System.Web.UI.WebControls;
  53. /*
  54.     void Application_BeginRequest(object sender, EventArgs e) {
  55.  
  56.         HttpRequest Request = HttpContext.Current.Request;
  57.         HttpResponse Response = HttpContext.Current.Response;
  58.         HttpServerUtility Server = HttpContext.Current.Server;
  59.         HttpContext Context = HttpContext.Current;
  60.         if (Request.Path.EndsWith(".fdl")) {
  61.             FileDownloadHandler handler=new FileDownloadHandler();
  62.             handler.ProcessRequest(HttpContext.Current);
  63.         }
  64.     }
  65.  */
  66. public class FileDownloadParameter {
  67.  
  68.     public static string Serialize(FileDownloadParameter cs) {
  69.         return XmlHelpers.SerializeObject(cs, typeof(FileDownloadParameter));
  70.     }
  71.     public static FileDownloadParameter Deserialize(string base64) {
  72.         return (FileDownloadParameter)XmlHelpers.DeserializeObject(base64, typeof(FileDownloadParameter));
  73.     }
  74.  
  75.     //public const string FileTypeQueryString = "filetype,ft,f";
  76.     //public const string FileNameconstQueryString = "filename,fn,n";
  77.     //public const string DownloadKeyconstQueryString = "downloadkey,key,k";
  78.  
  79.  
  80.     private string m_FileType;
  81.     public string FileType {
  82.         get { return m_FileType; }
  83.         set { m_FileType = value; }
  84.     }
  85.  
  86.     private string m_FileName;
  87.     public string FileName {
  88.         get { return m_FileName; }
  89.         set { m_FileName = value; }
  90.     }
  91.  
  92.     private string m_DownloadKey;
  93.     public string DownloadKey {
  94.         get { return m_DownloadKey; }
  95.         set { m_DownloadKey = value; }
  96.     }
  97.  
  98.  
  99.     private object m_FileData;
  100.     public object FileData {
  101.         get { return m_FileData; }
  102.         set { m_FileData = value; }
  103.     }
  104.  
  105.  
  106.     public FileDownloadParameter() {
  107.     }
  108.  
  109.     public FileDownloadParameter(string p_FileType,
  110.         string p_FileName,
  111.         string p_DownloadKey)
  112.         : this() {
  113.         this.m_FileType = p_FileType;
  114.         this.m_FileName = p_FileName;
  115.         this.m_DownloadKey = p_DownloadKey;
  116.  
  117.     }
  118.  
  119. }
  120. public class FileDownloader {
  121.  
  122.     public static void DownloadFile(FileDownloadParameter parameters) {
  123.         string url = GetDowloadFileUrl(parameters);
  124.         Response.Redirect(url, true);
  125.     }
  126.     public static void DownloadFile(string p_FileType,
  127.         string p_FileName,
  128.         object fileData) {
  129.         FileDownloadParameter p= new FileDownloadParameter();
  130.         p.FileType = p_FileType;
  131.         p.FileName = p_FileName;
  132.         p.FileData = fileData;
  133.  
  134.         DownloadFile(p);
  135.     }
  136.  
  137.     public static string GetDowloadFileUrl(FileDownloadParameter parameters) {
  138.         string key = Guid.NewGuid().ToString();
  139.         parameters.DownloadKey = key;
  140.         string url = "filedownload.aspx?k=" + key;
  141.         HttpContext.Current.Cache.Insert(key, parameters, null, DateTime.MaxValue, new TimeSpan(8, 0, 0));
  142.         return url;
  143.     }
  144.     public static string GetDowloadFileUrl(string p_FileType,
  145.         string p_FileName,
  146.         object fileData) {
  147.         FileDownloadParameter p= new FileDownloadParameter();
  148.         p.FileType = p_FileType;
  149.         p.FileName = p_FileName;
  150.         p.FileData = fileData;
  151.  
  152.         return GetDowloadFileUrl(p);
  153.     }
  154.     public static FileDownloadParameter GetDataFromKey(string downloadKey) {
  155.         return (FileDownloadParameter) HttpContext.Current.Cache[downloadKey];
  156.  
  157.     }
  158.     private static HttpResponse Response {
  159.         get { return HttpContext.Current.Response; }
  160.     }
  161. }
  162.  
  163. public class FileDownloadHandler : IHttpHandler {
  164.    
  165.     public void ProcessRequest (HttpContext context) {
  166.  
  167.        
  168.         HttpRequest Request = HttpContext.Current.Request;
  169.         HttpResponse Response = HttpContext.Current.Response;
  170.         HttpServerUtility Server = HttpContext.Current.Server;
  171.         HttpContext Context = HttpContext.Current;
  172.        
  173.         string key = Request.QueryString["k"];
  174.         if (string.IsNullOrEmpty(key))
  175.             return;
  176.  
  177.         FileDownloadParameter parameter = FileDownloader.GetDataFromKey(key);
  178.         if (parameter ==null)
  179.             return;
  180.  
  181.         string id = parameter.DownloadKey;
  182.         string filetype = parameter.FileType;
  183.         string filename = parameter.FileName;
  184.         object fileData = parameter.FileData;
  185.  
  186.         if (string.IsNullOrEmpty(filename))
  187.             filename = "export." + filetype;
  188.  
  189.         Response.ClearHeaders();
  190.         if (filetype == "csv") {
  191.             string csvData = fileData.ToString();
  192.             Response.ContentType = "text/csv";
  193.             Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
  194.             Response.AddHeader("Content-Length", csvData.Length.ToString());
  195.             Response.Write(csvData);
  196.         }
  197.         if (filetype == "txt") {
  198.             string csvData = fileData.ToString();
  199.             Response.ContentType = "text/text";
  200.             Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
  201.             Response.AddHeader("Content-Length", csvData.Length.ToString());
  202.             Response.Write(csvData);
  203.         }
  204.         if (filetype == "xls") {
  205.             MemoryStream contents = fileData as MemoryStream;
  206.             contents.Position = 0;
  207.             Response.ContentType = "application/vnd.ms-excel";
  208.             Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
  209.             Response.BinaryWrite(contents.ToArray());
  210.         }
  211.         if (filetype == "rtf") {
  212.             MemoryStream contents = fileData as MemoryStream;
  213.             contents.Position = 0;
  214.             Response.ContentType = "application/vnd.ms-word";
  215.             Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
  216.             Response.BinaryWrite(contents.ToArray());
  217.         }
  218.         if (filetype == "pdf") {
  219.             MemoryStream contents = fileData as MemoryStream;
  220.             contents.Position = 0;
  221.             Response.ContentType = "application/pdf";
  222.             Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
  223.             Response.BinaryWrite(contents.ToArray());
  224.         }
  225.  
  226.         Response.End();
  227.    
  228.     }
  229.  
  230.     public bool IsReusable {
  231.         get {
  232.             return true;
  233.         }
  234.     }
  235.  
  236. }
Add Comment
Please, Sign In to add comment