Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void DownloadTemplate(string FileName)
   {
       try
       {

           String strFile = String.Empty;
           String[] filename;

           strFile = Server.MapPath(ConfigurationManager.AppSettings["TemplatePath"].ToString()) + FileName;

           filename = strFile.Split('\\');

           Response.Clear();
           Response.Buffer = true;// Read the original file from diskFileStream *
           Response.AddHeader("Content-Disposition", "attachment; filename=" + filename[filename.Length-1]);


           String ext = String.Empty;
           if (strFile.IndexOf(".") > 0)
               ext = (strFile.Split('.'))[1];

           if (ext.ToLower() == "txt")
               Response.ContentType = "text/html";
           if (ext.ToLower() == "xls")
               Response.ContentType = "xls/html";
           if (ext.ToLower() == "pdf")
               Response.ContentType = "application/pdf";

           ////combine the path and file name
           if (File.Exists(strFile))//open the file and process it
           {
               FileStream sourceFile = new FileStream(strFile, FileMode.Open, FileAccess.Read);
               long FileSize;
               FileSize = sourceFile.Length;
               Response.AddHeader("Content-Length", FileSize.ToString()); //*
               byte[] getContent = new byte[(int)FileSize];
               sourceFile.Read(getContent, 0, (int)FileSize);

               sourceFile.Close();
               Response.BinaryWrite(getContent);
           }
           Common.UserPageAudit(Session["User"].ToString(), "Download Templates", Session["ROLE"].ToString(), strFile + " Template downloaded");
       }
       catch (Exception ex)
       {
           Response.Redirect("../CustomError.aspx?Err=" + ex.Message.ToString(), false);
       }
   }
Posted
Comments
Sergey Alexandrovich Kryukov 23-Dec-13 1:41am    
Who told you they should?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900