Tuesday, December 25, 2012

Download file


               
                       

 hdnpath.Value = Server.MapPath("~/FilesManager/Calendar/" + result[0].EVENT_Attachment_Path);
 private string ReturnExtension(string fileExtension)
        {
            switch (fileExtension)
            {
               
                case ".pdf":
                    return "text/pdf";
                case ".doc":
                case ".docx":
                    return "application/ms-word";
                case ".xls":
                case ".xlsX":
                    return "application/x-msexcel";
                default:
                    return "application/octet-stream";
            }

        }
 protected void btn_attachment_Click(object sender, EventArgs e)
        {
                DownloadFile(hdnpath.Value,true);
        }

        private void DownloadFile(string fname, bool forceDownload)
        {
            try
            {
               
                string filepath = fname;
                FileInfo file = new FileInfo(filepath);
                if (file.Exists)
                {
                    Response.ClearContent();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = ReturnExtension(file.Extension.ToLower());
                    Response.TransmitFile(file.FullName);
                    Response.End();

                }
            }
            catch (Exception ex)
            { }
        }

Copyright © 2009 Angel