Thursday 3 September 2015

Download Word, Pdf Files using Asp.Net

Download Word Document
string sourcefile = Server.MapPath("~/WordDoc/ " + filename + ".doc");
if (File.Exists(sourcefile))
{
System.IO.FileInfo downloadFile = new System.IO.FileInfo(sourcefile);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/odc";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", downloadFile.Name));
HttpContext.Current.Response.AddHeader("Content-Length", downloadFile.Length.ToString());
HttpContext.Current.Response.WriteFile(downloadFile.FullName);
//HttpContext.Current.Response.End();
//HttpContext.Current.Response.Close();
}
 In above example, we are downloading a .doc file.
 Here are some of the most common content types replace in above code:
  •  .htm, .html     Response.ContentType = "text/HTML";
  • .txt    Response.ContentType = "text/plain";
  • .doc, .rtf, .docx    Response.ContentType = "Application/msword";
  • .xls, .xlsx    Response.ContentType = "Application/x-msexcel";
  • .jpg, .jpeg    Response.ContentType = "image/jpeg";
  • .gif    Response.ContentType =  "image/GIF";
  • .pdf    Response.ContentType = "application/pdf";

No comments:

Post a Comment