Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Data.Sql;
- using System.Data.SqlClient;
- using System.Data.OleDb;
- using System.Data.Common;
- using System.Data.SqlTypes;
- using System.Data.ProviderBase;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Net;
- using System.Diagnostics;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Xml.Schema;
- using System.Xml.XPath;
- using System.Xml.Xsl;
- using System.Configuration;
- using System.Globalization;
- using System.Reflection;
- using System.Data.SqlClient;
- using System.Data.Common;
- using System.Drawing;
- using System.Web;
- using System.Web.Util;
- using System.Web.UI;
- using System.Web.SessionState;
- using System.Web.Security;
- using System.Web.Profile;
- using System.Web.Mail;
- using System.Web.Handlers;
- using System.Web.Hosting;
- using System.Web.DataAccess;
- using System.Web.Configuration;
- using System.Web.Compilation;
- using System.Web.Caching;
- using System.Web.Administration;
- using System.Web.Services;
- using System.Web.Services.Configuration;
- using System.Web.Services.Description;
- using System.Web.Services.Protocols;
- using System.Web.UI.WebControls;
- /*
- void Application_BeginRequest(object sender, EventArgs e) {
- HttpRequest Request = HttpContext.Current.Request;
- HttpResponse Response = HttpContext.Current.Response;
- HttpServerUtility Server = HttpContext.Current.Server;
- HttpContext Context = HttpContext.Current;
- if (Request.Path.EndsWith(".fdl")) {
- FileDownloadHandler handler=new FileDownloadHandler();
- handler.ProcessRequest(HttpContext.Current);
- }
- }
- */
- public class FileDownloadParameter {
- public static string Serialize(FileDownloadParameter cs) {
- return XmlHelpers.SerializeObject(cs, typeof(FileDownloadParameter));
- }
- public static FileDownloadParameter Deserialize(string base64) {
- return (FileDownloadParameter)XmlHelpers.DeserializeObject(base64, typeof(FileDownloadParameter));
- }
- //public const string FileTypeQueryString = "filetype,ft,f";
- //public const string FileNameconstQueryString = "filename,fn,n";
- //public const string DownloadKeyconstQueryString = "downloadkey,key,k";
- private string m_FileType;
- public string FileType {
- get { return m_FileType; }
- set { m_FileType = value; }
- }
- private string m_FileName;
- public string FileName {
- get { return m_FileName; }
- set { m_FileName = value; }
- }
- private string m_DownloadKey;
- public string DownloadKey {
- get { return m_DownloadKey; }
- set { m_DownloadKey = value; }
- }
- private object m_FileData;
- public object FileData {
- get { return m_FileData; }
- set { m_FileData = value; }
- }
- public FileDownloadParameter() {
- }
- public FileDownloadParameter(string p_FileType,
- string p_FileName,
- string p_DownloadKey)
- : this() {
- this.m_FileType = p_FileType;
- this.m_FileName = p_FileName;
- this.m_DownloadKey = p_DownloadKey;
- }
- }
- public class FileDownloader {
- public static void DownloadFile(FileDownloadParameter parameters) {
- string url = GetDowloadFileUrl(parameters);
- Response.Redirect(url, true);
- }
- public static void DownloadFile(string p_FileType,
- string p_FileName,
- object fileData) {
- FileDownloadParameter p= new FileDownloadParameter();
- p.FileType = p_FileType;
- p.FileName = p_FileName;
- p.FileData = fileData;
- DownloadFile(p);
- }
- public static string GetDowloadFileUrl(FileDownloadParameter parameters) {
- string key = Guid.NewGuid().ToString();
- parameters.DownloadKey = key;
- string url = "filedownload.aspx?k=" + key;
- HttpContext.Current.Cache.Insert(key, parameters, null, DateTime.MaxValue, new TimeSpan(8, 0, 0));
- return url;
- }
- public static string GetDowloadFileUrl(string p_FileType,
- string p_FileName,
- object fileData) {
- FileDownloadParameter p= new FileDownloadParameter();
- p.FileType = p_FileType;
- p.FileName = p_FileName;
- p.FileData = fileData;
- return GetDowloadFileUrl(p);
- }
- public static FileDownloadParameter GetDataFromKey(string downloadKey) {
- return (FileDownloadParameter) HttpContext.Current.Cache[downloadKey];
- }
- private static HttpResponse Response {
- get { return HttpContext.Current.Response; }
- }
- }
- public class FileDownloadHandler : IHttpHandler {
- public void ProcessRequest (HttpContext context) {
- HttpRequest Request = HttpContext.Current.Request;
- HttpResponse Response = HttpContext.Current.Response;
- HttpServerUtility Server = HttpContext.Current.Server;
- HttpContext Context = HttpContext.Current;
- string key = Request.QueryString["k"];
- if (string.IsNullOrEmpty(key))
- return;
- FileDownloadParameter parameter = FileDownloader.GetDataFromKey(key);
- if (parameter ==null)
- return;
- string id = parameter.DownloadKey;
- string filetype = parameter.FileType;
- string filename = parameter.FileName;
- object fileData = parameter.FileData;
- if (string.IsNullOrEmpty(filename))
- filename = "export." + filetype;
- Response.ClearHeaders();
- if (filetype == "csv") {
- string csvData = fileData.ToString();
- Response.ContentType = "text/csv";
- Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
- Response.AddHeader("Content-Length", csvData.Length.ToString());
- Response.Write(csvData);
- }
- if (filetype == "txt") {
- string csvData = fileData.ToString();
- Response.ContentType = "text/text";
- Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
- Response.AddHeader("Content-Length", csvData.Length.ToString());
- Response.Write(csvData);
- }
- if (filetype == "xls") {
- MemoryStream contents = fileData as MemoryStream;
- contents.Position = 0;
- Response.ContentType = "application/vnd.ms-excel";
- Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
- Response.BinaryWrite(contents.ToArray());
- }
- if (filetype == "rtf") {
- MemoryStream contents = fileData as MemoryStream;
- contents.Position = 0;
- Response.ContentType = "application/vnd.ms-word";
- Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
- Response.BinaryWrite(contents.ToArray());
- }
- if (filetype == "pdf") {
- MemoryStream contents = fileData as MemoryStream;
- contents.Position = 0;
- Response.ContentType = "application/pdf";
- Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
- Response.BinaryWrite(contents.ToArray());
- }
- Response.End();
- }
- public bool IsReusable {
- get {
- return true;
- }
- }
- }
Add Comment
Please, Sign In to add comment