Monday, June 11, 2012

File Direct Download

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;namespace
{
WebApplication7public partial class WebForm1 : System.Web.UI.Page{

{
 
DownloadFile(
}
 

{
protected void Page_Load(object sender, EventArgs e)"sss1.docx");public void DownloadFile(string fileName)try{


FileExt = FileExt.ToUpper();


{









string strfileName = GetDownloadPath(fileName);string FileExt = strfileName.Substring(strfileName.LastIndexOf(".") + 1);FileInfo fInfo = new FileInfo(strfileName);if (fInfo.Exists)HttpContext.Current.Response.ClearContent();HttpContext.Current.Response.ClearHeaders();HttpContext.Current.Response.Buffer = true;if (FileExt == "XLS")HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";else if (FileExt == "DOC")HttpContext.Current.Response.ContentType = "application/ms-word";else if (FileExt == "PDF")HttpContext.Current.Response.ContentType = "application/pdf";else




}
HttpContext.Current.Response.ContentType = "application/octet-stream";HttpContext.Current.Response.AddHeader("Content-Length", fInfo.Length.ToString());HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");HttpContext.Current.Response.TransmitFile(fInfo.FullName);HttpContext.Current.Response.Flush();else{

}

}

}
 
 
 
 

{


System.IO.


}
}
}
throw new Exception("File Not Available. File Name:" + fileName);catch (Exception ex) { throw ex; }public string GetDownloadPath(string fileName)string strDirectoryPath = "D:\\sample";if (!(Directory.Exists(strDirectoryPath)))Directory.CreateDirectory(strDirectoryPath);string strfileName = strDirectoryPath + "\\" + fileName;return strfileName;