ASP.NET File Downloader
Idea shared by Administrator - April 3, 2018 at 8:13 PM
Proposed
Employee Post
HOW TO:
either call it from a button or Redirect
FileDownloader.ashx?file=export/documentname.pdf
 
From the Visual Studio Click on Add New Item --> Toolbox--> Select --> Generic Handler
Name it --> FileDownloader.ashx
and replace the template code with this one.
<%@ WebHandler Language="C#" Class="FileDownloader" %>

using System;
using System.IO;
using System.Web;

public class FileDownloader : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
                
        //- Make sure the parameter carries the file name.
        if (context.Request.QueryString["file"] != null)
        {   
            //- File name to Download
            string _sFileNameToDownload = context.Request.QueryString["file"].ToString();                        
            
            //- Validate file and path
            if (!string.IsNullOrEmpty(_sFileNameToDownload) && File.Exists(context.Server.MapPath(_sFileNameToDownload)))
            {
                context.Response.Clear();
                context.Response.ContentType = GetFileContent_Type(_sFileNameToDownload);
                context.Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(_sFileNameToDownload));
                context.Response.WriteFile(context.Server.MapPath(_sFileNameToDownload));
                //- Flush to Client (Must turn the Flush=True in Page_Load event)
                context.Response.Flush();                                
                //- Complete
                context.Response.End();                
            }
            else
            {
                //- Oops no file was found.
                context.Response.ContentType = "text/plain";
                context.Response.Write("File was not found!");
                context.Response.Flush();                
            }                        
        }
    } 
    public bool IsReusable {
        get {
            return false;
        }
    }
    //*********************************************************************************************************************************************
    //- Get the File Contenttype based up on the file format extension passed.
    //*********************************************************************************************************************************************
    private string GetFileContent_Type(string pFileNameToDownLoad)
    {
        string _sFileRealContent_Type = "";
        if (pFileNameToDownLoad.Trim().ToLower().EndsWith(".pdf"))        
        {
            _sFileRealContent_Type = "application/pdf";
        }
        else if (pFileNameToDownLoad.Trim().ToLower().EndsWith(".doc") || pFileNameToDownLoad.Trim().ToLower().EndsWith(".docx"))
        {
            _sFileRealContent_Type = "application/msword";
        }
        else if (pFileNameToDownLoad.Trim().ToLower().EndsWith(".xls") || pFileNameToDownLoad.Trim().ToLower().EndsWith(".xlsx"))
        {
            _sFileRealContent_Type = "application/vnd.ms-excel";
        }
        else if (pFileNameToDownLoad.Trim().ToLower().EndsWith(".jpg") || pFileNameToDownLoad.Trim().ToLower().EndsWith(".jpeg"))
        {
            _sFileRealContent_Type = "image/jpeg";
        }
        else if (pFileNameToDownLoad.Trim().ToLower().EndsWith(".gif"))
        {
            _sFileRealContent_Type = "image/gif";
        }
        else if (pFileNameToDownLoad.Trim().ToLower().EndsWith(".html") || pFileNameToDownLoad.Trim().ToLower().EndsWith(".htm"))
        {
            _sFileRealContent_Type = "text/html";
        }
        else
        {   
            _sFileRealContent_Type = "application/octet-stream";
        }
        return _sFileRealContent_Type;
    }    
}
Happy coding!

Reply to Thread