Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to download in asp.net C# website? please type with code and namespace.
Posted

This question makes no sense. You don't need code to offer a download from a website, you just need a link to the file. You can write code that writes a file to the Response object, that code is easy to find in your books or online if you need it.
 
Share this answer
 
C#
 public static string ServerMapPath(string path)
    {
        return HttpContext.Current.Server.MapPath(path);
    }
    public static HttpResponse GetHttpResponse()
    {
        return HttpContext.Current.Response;
    }
    public static void DownLoadFileFromServer(string fileName)
    {
        //This is used to get Project Location.
        try
        {
            string filePath = ServerMapPath(fileName);
            //This is used to get the current response.
            HttpResponse res = GetHttpResponse();
            res.Clear();
            res.AppendHeader("content-disposition", "attachment; filename=" + filePath);
            res.ContentType = "application/octet-stream";
            res.WriteFile(filePath);
            res.Flush();
            res.End();
        }
        catch (Exception ex)
        {
           
        }
    }

 protected void Button2_Click(object sender, EventArgs e)
    {
DownLoadFileFromServer("~/Storage/" + txtname.Text);
}





file need to be in same directory of website in some folder int this case folder name is Storage
 
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