C# 下载的excel文件未打开
我正在尝试将excel文件下载到本地系统中的位置,但打开该文件时,出现错误: 文件格式或扩展名不匹配 虽然在后端的文件是相同的扩展名,但我得到的错误 PFB代码:C# 下载的excel文件未打开,c#,asp.net,.net,C#,Asp.net,.net,我正在尝试将excel文件下载到本地系统中的位置,但打开该文件时,出现错误: 文件格式或扩展名不匹配 虽然在后端的文件是相同的扩展名,但我得到的错误 PFB代码: string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.ContentType = ContentType; Response.AppendHeader("Content-Disposition
string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename="+downloadedFileName);
downloadedFileName= "Myfile.xlsx"
它将另存为details.xls
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Details.xls"));
Response.ContentType = "application/ms-excel";
出现此错误是因为您没有使用
Response.TransmitFile()
。您的代码应该是:
string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename="+downloadedFileName);
Response.TransmitFile(FilePath); // full path here
请确定:文件大小是否匹配?手动从服务器复制时是否可以打开文件?我没有看到
响应。写…
,你能粘贴代码吗?我想你没有提到文件扩展名。i、 e.xlsx或。xls@PatrickHofman:文件已保存到本地系统。当我双击它打开时,我得到的错误不在代码中。后端的文件很好。您必须为.xlsx.
设置ContentType
:application/vnd.openxmlformats oficedocument.spreadsheetml.sheet
,为.xls
@Taosique设置application/vnd.ms excel
:我只为.xlsx设置了tanks Taosique,但是你能修改代码吗?这样我就可以问用户文件保存的位置。@Aquarius24:我想你是在问客户端的问题。这正是您的浏览器决定使用的方式。例如,Chrome和IE会将其保存在下载文件夹中。这是无法从服务器控制的。对<代码>响应。TransmitFile()获取服务器上文件的路径。