Wednesday, 2 April 2014

ASP.NET, C#.NET - How to download a file in ASP.NET using C#.NET?

Hi All,
By using the following simple code snippet page you can download any type of file in ASP.NET using C#.NET.
Please observe the steps in the following code to know how the 'file downloading process' is happening.


FileManagement.aspx

<%@ Page Language="C#" %>
 
<!DOCTYPE html>
 
<script runat="server">
 
    protected void Page_Load(object sender, EventArgs e)
    {
        var fullFilePath = @"D:\Krish\gallery (1).jpg";
        DownloadFile(fullFilePath, System.IO.Path.GetFileName(fullFilePath));
    }  
    
    private void DownloadFile(string fullFilePhysicalPath, string downloadbleFileName)
    {
 
        // Create New instance of FileInfo
        System.IO.FileInfo file = new System.IO.FileInfo(fullFilePhysicalPath);
 
        // Checking if file exists
        if (file.Exists)
        {
            // Clear the content of the response
            Response.ClearContent();
 
            // Add the file name and attachment
            Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadbleFileName);
 
            // Add the file size into the response header
            Response.AddHeader("Content-Length", file.Length.ToString());
 
            // Set the ContentType
            Response.ContentType = GetFileExtension(file.Extension.ToLower());
 
            // Write the file into the response 
            // ASP.NET 2.0 - TransmitFile
            // ASP.NET 1.1 - WriteFile
            Response.TransmitFile(file.FullName);
 
            // End the response
            Response.End();          
        }
    }
 
    private string GetFileExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

No comments: