【问题标题】:Repsonse.Transmitfile(); Able to Save but cannot OpenResponse.Transmitfile();可以保存但不能打开
【发布时间】:2012-04-05 02:40:27
【问题描述】:

我正在尝试使用发送 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',因为文件格式或文件扩展名无效...”错误。
我目前正在以调试模式从 VS2010 运行该站点。

有人知道为什么它会让我保存,但不能直接打开吗?

编辑
Chrome 只是下载它。 FF 尝试打开它但给出错误The file you are trying to open, 'myFile.xlsx.xls', is in a different format than specified by the file extension... 我可以选择打开它并且它成功打开,但处于只读模式。
所以,这里发生了一些时髦的事情。
文件名 = "myFile.xlsx"

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

【问题讨论】:

  • 你在FF或Chrome中试过了吗?这绝对令人困惑,因为 IE 应该只是将您的文件保存到临时位置并从那里打开它。

标签: c# asp.net response.transmitfile


【解决方案1】:

这是因为您的 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();

【讨论】:

  • 嗯,这很尴尬。但是我修复了它并且错误仍然存​​在。我也尝试过使用“octet-stream”并得到同样的错误。
  • 我有写权限,管理员模式下的IE也不行。
【解决方案2】:

我曾经遇到过同样的问题,通过修改应用服务器发送的缓存指令解决了,在本例中是 IIS。有时您会在应用程序级别添加标头,以防止文件在某些​​情况下被保存,例如在 HTTPS 连接中。确保您没有提出相互矛盾的说明。

【讨论】:

  • 这就是我AFAIK的情况。当我删除 the cache stuff 时,一切都恢复正常了。我以前没有 Content-Length 标头,在应用一个之后,我可以再次禁用缓存。
【解决方案3】:

我遇到了类似的问题。

原来我是从流的末尾读取的,所以字节数组没有被任何东西填满。

在读取流 (sourceFile.Read(getContent, 0, (int)sourceFile.Length);) 之前将流位置设置为 0 (sourceFile.Position=0;) 为我修复了它。

看看有没有帮助。

至于“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.Position=0;

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

【讨论】:

    【解决方案4】:

    巧合的是,我正处于类似事件的中间......

    用于 xlsx 的 MIME 类型是 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,请参阅 this question

    【讨论】:

      【解决方案5】:

      我被这个问题难住了,尤其是对于较长的文件。

      在我们的案例中,使用 Content-Length 标头解决了这个问题。一旦内容超过一定长度,就会出现此问题。

      示例(其中 tempsbldr 是 StringBuilder):

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

      【讨论】:

        猜你喜欢
        • 2013-04-10
        • 1970-01-01
        • 1970-01-01
        • 2017-11-04
        • 2017-11-08
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多