Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have a .aspx page where i have given a button control named download, on clicking download button it send an excel file to browser for download.On opening the excel it shows content properly.


As per my requirement i want fileupload control on the same page. I just dragged and dropped the fileupload control to design page and run the program. now when i download the excel and opens it,it shows the file is not in recognizable format.

if i removes the fileupload control, it works fine.
What can be the reason

Code on button click is as following
C#
protected void Button2_Click1(object sender, EventArgs e)
       {
           string filePath = Label117.Text;
           FileInfo file = new FileInfo(filePath);
           if (file.Exists)
           {
               Response.Clear();

               Response.ClearHeaders();

               Response.ClearContent();

               Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

               Response.AddHeader("Content-Length", file.Length.ToString());

               Response.ContentType = "application/vnd.xls";

               Response.Flush();

               Response.TransmitFile(file.FullName);

               Response.End();
           }
       }
Posted
Updated 24-Jul-13 23:33pm
v3

Hi Mahakaal,

If you are sure that the extension is always in Excel format, then do the modification as follows:

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name + ".XLS"); 


You are missing the Excel format tag at the end.

Let me know if this helped you or not.

Happy coding...:)

Thanks,
RelicV
 
Share this answer
 
Comments
mahakaal 25-Jul-13 7:25am    
i did what you said but still the problem is same.

if i remove the fileupload control than it works fine.
C#
Response.ContentType = "application/xls";
 
Share this answer
 
Comments
mahakaal 25-Jul-13 8:33am    
dear i had already tried your solution but it's not working .

i have the solution to server my purpose.
which is as following if i simply use the below code instead of using the code mentioned in my problem

protected void Button2_Click1(object sender, EventArgs e)
{
string filePath = Label117.Text;
FileInfo file = new FileInfo(filePath);

if (file.Exists)
{

Response.Redirect("~/ExcelFiles/" + file.Name);

}


}

but i want to know why this problem is coming if i use previous mentioned code.

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