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."); }
       }
}



 


Comments




Leave a Reply

    About the Author

    The author have 12+ years experience in IT Industry with varying job roles from system developer/analyst/consultant positions. Majority of her development, implementation and systems support background focus on Financial/Banking/Credit Solutions.

    Archives

    November 2010
    October 2010

    Categories

    All

    RSS Feed