Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am having few files of format (pdf,.xls,.doc etc) but on downloading from web server I am getting error msg as Internet Explorer cannot download x.aspx from the site. Internet explorer cannot open the site. The requested site is either unavailable or try again later.Its an https site.In local its fine and all basic softwares are there in server too. All are set with in the tables (template name-x.xls/x.pdf etc are in the table)
Code as follows:
C#
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);
           }
Posted
Updated 18-Feb-14 3:44am
v4
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 18:27pm    
If there is the problem, it's not inside this code fragment. It depends on how you set up the site...
—SA
Rahul 105 18-Feb-14 23:05pm    
I saw many saying to clear the cache header in the above code:
Response.ClearHeaders();
Response.Clear();

Response.Buffer = true;
Response.ContentType = “application/csv”;
Response.AddHeader(“Content-Disposition”, “attachment;filename=report.csv”);
Response.Write(strData);
Response.Flush();
Response.End();

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