Buddies, I want to design a simple file download button in deafult.aspx. Can anyone provide me the detailed C# code behind it? Thanks in advance.

Recommended Answers

All 2 Replies

Here is one approach:

public void btnDownload_Click(object source, EventArgs e)
{
   Response.Redirect("url_to_the_file_you_want");
}

Although this will OPEN a file.

Here is an approach to download a file:

string URL = "your_url";
FileInfo fileInfo = new FileInfo(URL);

if (fileInfo.Exists)
{
 Response.Clear();
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
 Response.ContentType = "application/octet-stream";
 Response.Flush();
 Response.WriteFile(fileInfo.FullName);
}
else
{
//display error finding file message.
}

In addition be sure to import:

System.IO.FileInfo

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.