gpt4 book ai didi

c# - 响应.Transmitfile();可以保存但打不开

转载 作者:太空狗 更新时间:2023-10-29 18:32:24 31 4
gpt4 key购买 nike

我正在尝试通过使用发送一个 xlsx 文件

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/vnd-ms.excel";
Response.TransmitFile(file.FullName);
Response.End();

弹出 IE 对话框,我可以成功保存文件,然后从文件夹中打开它,效果很好,很漂亮。但是,如果我在 IE 对话框中单击“打开”,则会收到“无法下载 myFile.xlsx”。我单击“重试”并打开 Excel,但弹出“Excel 无法打开文件‘myFile.xlsx’,因为文件格式或文件扩展名无效...”错误。
我目前正在 Debug模式下从 VS2010 运行网站。

有谁知道为什么让我保存,但不直接打开?

编辑
Chrome 只是下载它。 FF 尝试打开它但给出错误无论如何,它成功打开,但处于只读模式。
所以,这里发生了一些奇怪的事情。
文件名 = "我的文件.xlsx"

编辑2
这是在 IE 9 中。我还尝试了 octet-streamapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet 作为 ContentType。

最佳答案

是因为你的ContentType不对。使用

Response.ContentType = "application/vnd.ms-excel";

编辑

如果不行,你能试试吗

FileStream sourceFile = new FileStream(file.FullName, FileMode.Open);
float FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Length", getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(getContent);
Response.Flush();
Response.End();

关于c# - 响应.Transmitfile();可以保存但打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9790043/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com