Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I wants code that helps for downloading files in asp.net

How to download file Asynchrnously..

Please Help me to be a good coder.
Posted

Hi,
this link will help u

problem with asynchronously downloading in asp.net[^]

best luck
 
Share this answer
 
C#
public static void download(string fileName, string filepath)
    {
        string userAgent = HttpContext.Current.Request.Headers.Get("User-Agent");
        string f = HttpContext.Current.Server.UrlDecode(fileName);
        if (userAgent.Contains("MSIE 7.0"))
        {
            //for IE
            f = fileName.Replace(" ", "%20");
        }
        else
        {
            //do nothing
        }
        fileName = HttpContext.Current.Server.UrlDecode(fileName);
        System.IO.FileStream liveStream = new System.IO.FileStream(HttpContext.Current.Server.MapPath(filepath), System.IO.FileMode.Open, System.IO.FileAccess.Read);
        byte[] buffer = new byte[Convert.ToInt32(liveStream.Length)];
        liveStream.Read(buffer, 0, Convert.ToInt32(liveStream.Length));
        liveStream.Close();

        HttpContext.Current.Response.Clear();

        if (fileName.EndsWith(".csv"))
        {
            HttpContext.Current.Response.ContentType = "application/csv";
        }
        else
        {
            HttpContext.Current.Response.ContentType = "application/octet-stream";
        }
        
        HttpContext.Current.Response.AddHeader("Content-Length", buffer.Length.ToString());
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + f + "\"");
        HttpContext.Current.Response.BinaryWrite(buffer);
        HttpContext.Current.Response.End();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900