Monday, January 30, 2012

Best Extension Methods: Force Download a File

Below Extension method is for Force Download a file
 // full path to your file
    //var yourFilePath = HttpContext.Current.Request.PhysicalApplicationPath + "Files\yourFile.jpg";
    // save downloaded file as (name)
    //var saveFileAs = "yourFile.jpg";

    // start force download of your file
    //Response.ForceDownload(yourFilePath, saveFileAs);    
    public static void ForceDownload(this HttpResponse Response, string fullPathToFile, string outputFileName)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=" + outputFileName);
        Response.WriteFile(fullPathToFile);
        Response.ContentType = "";
        Response.End();
    }

Compiled By: Rajesh Rolen

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates