下载.docx文件时,它已下载但未打开给出错误... [英] When Download .docx file, it's downloaded but not opened Gives error...

查看:94
本文介绍了下载.docx文件时,它已下载但未打开给出错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我在gridview row命令中使用了以下代码片段来从服务器下载文件...



Hi friends,
I have used the following code snippet at gridview row command to down load a file from server...

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();
}

}

}





发生的每一件事都是正确的但是当我尝试下载.docx扩展文件时,文件下载成功,但是当尝试打开时出现如下错误:



办公室打开xml文件(文件名) )无法打开,因为内容有问题详细信息:文件已损坏且无法打开。



为什么会发生这种情况?

请帮助我。



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.

推荐答案

请替换

Response.AppendHeader(content-disposition,attachment; filename = + name);

with

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



如果这不起作用,请还原。将有助于其他一些工作。
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.


这篇关于下载.docx文件时,它已下载但未打开给出错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆