Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi friends,
I have used the following code snippet at gridview row command to down load a file from server...

C#
if (e.CommandName == "downLoad")
{
DataTable dt = new DataTable();
Int32 id = Convert.ToInt32(e.CommandArgument);
p.Admin_Flag = 8;
p.Id = id;
p.DateFrom_Sorting = System.DateTime.Now;
p.DateTo_Sorting = System.DateTime.Now;
dt = b.Job_Applied_GridShow(p);
if (dt.Rows.Count > 0)
{
string filepath = "~/" + dt.Rows[0]["file_path"].ToString();


//.......................................................
string path = Server.MapPath(filepath);
//get physical file path from server 
string name = Path.GetFileName(path);
//get file name 
string ext = Path.GetExtension(path);
//get file extension 
string type = "";
// set known types based on file extension 
if (ext != null)
{
switch (ext.ToLower())
{

case ".pdf":
type = "Application/pdf";
break;
case ".doc":
type = "Application/msword";
break;
case ".docx":
type = "Application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case ".rtf":
type = "Application/msword";
break;
Default:
type = "";
break;
}
}
Response.AppendHeader("content-disposition", "attachment; filename=" + name);
if (type != "")
{
Response.ContentType = type;
Response.Flush();
Response.WriteFile(path);
Response.End();
}

}

}


Every thing happening right but when i try to download .docx extension file, the file downloaded successfully but when try to open gives an error like :

"The office open xml file (file name) can not be opened because there are problem with the content" Detail : The file is currupt and can not be opened.

Why this is happening?
Please help me.
Posted
Updated 2-Feb-14 20:29pm
v2

1 solution

Please replace
Response.AppendHeader("content-disposition", "attachment; filename=" + name);
with
Response.AppendHeader("content-disposition", "attachment; filename=" + name + ext.ToLower());

Please revert in case this doesn't work. Will help with some other work arounds.
 
Share this answer
 

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