1. create a hyperlink in your web page
<asp:HyperLink ID="hyperLinkDownloadFile" runat="server">Download this file</asp:HyperLink>
2. in your code-behind
hyperLinkFileGen.Text = "click on this link to download the document";
hyperLinkFileGen.NavigateUrl = "downloadpage.aspx?file=yourDocument.ext";
3. create downloadpage.aspx
using System.IO;
public partial class downloadpage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filename = Request["file"].ToString();
fileDownload(Server.MapPat(filename));
}
private void fileDownload(string filename)
{
System.IO.FileInfo theFile= new System.IO.FileInfo(fileName);
if (file.Exists) {
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + theFile.Name);
Response.AddHeader("Content-Length", theFile.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(theFile.FullName);
Response.End();
} else { Response.Write("This file does not exist."); }
}
}
<asp:HyperLink ID="hyperLinkDownloadFile" runat="server">Download this file</asp:HyperLink>
2. in your code-behind
hyperLinkFileGen.Text = "click on this link to download the document";
hyperLinkFileGen.NavigateUrl = "downloadpage.aspx?file=yourDocument.ext";
3. create downloadpage.aspx
using System.IO;
public partial class downloadpage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filename = Request["file"].ToString();
fileDownload(Server.MapPat(filename));
}
private void fileDownload(string filename)
{
System.IO.FileInfo theFile= new System.IO.FileInfo(fileName);
if (file.Exists) {
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + theFile.Name);
Response.AddHeader("Content-Length", theFile.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(theFile.FullName);
Response.End();
} else { Response.Write("This file does not exist."); }
}
}