Chrome将文件附件误解为文档

我有一个链接转到一个PHP脚本,该脚本生成一个CSV文件以供下载和保存。 我在响应中生成Content-Disposition和Content-Type标题。 除Chrome(v19)以外,每个浏览器都可以下载文件。

链接是这样的:

http://hostname.com/controller/action/export

从该请求返回的标题是:

Cache-control:no-cache, must-revalidate
Connection:Keep-Alive
Content-Disposition:attachment; filename=2012-03-14.csv
Content-Encoding:gzip
Content-Length:521
Content-Type:application/vnd.ms-excel
Date:Thu, 15 Mar 2012 05:17:55 GMT
Expires:Mon, 26 Jul 1997 05:00:00 GMT
Keep-Alive:timeout=5, max=92
P3P:CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Pragma:no-cache
Server:Apache/2.2.11 (Win32) mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.9
Vary:Accept-Encoding
X-Powered-By:PHP/5.2.9

Chrome开发人员工具显示网络连接已Canceled ,控制台窗口显示以下错误:

Resource interpreted as Document but transferred with MIME type application/vnd.ms-excel:

我试过使用Content-Type的不同值,我关闭了Cache-Control和Content-Type标题。 我已经使用Javascript location.href=<a>标签, <form action="POST"> ,关闭了Gzip压缩以及其他各种方法来尝试让Chrome实际下载文件。

每个其他浏览器都会将该文件下载得很好,所以我的问题是:什么导致Chrome将请求解释为显示的“文档”而不是附件? 是否有另一个我失踪的标题或列表中的标题令人困惑?

编辑:这是PHP代码的请求,虽然它有点长:

function renderHeaders($filename = null) {
  header("Content-Type: application/vnd.ms-excel");
  header("Cache-Control: public, must-revalidate, max-age=0");
  if (is_string($filename)) {
    $this->setFilename($filename);
  }
  if ($this->filename === null) {
    $this->filename = 'Data.csv';
  }
  if ($this->filename) {
    header("Content-disposition: attachment; filename=".$this->filename);
  }
}

function render($outputHeaders = true, $to_encoding = null, $from_encoding = "auto") {
  if ($outputHeaders) {
    $this->renderHeaders();
  }
  if ($this->_tmpFile) {
    rewind($this->buffer);
    $output = '';
    while (!feof($this->buffer)) {
      $output .= fread($this->buffer, 8192);
    }
    fclose($this->buffer);
  } else {
    rewind($this->buffer);
    $output = stream_get_contents($this->buffer);
  }
  // get around excel bug (http://support.microsoft.com/kb/323626/)
  if (substr($output,0,2) == 'ID') {
    $pos = strpos($output, $this->delimiter);
    if ($pos === false) {
      $pos = strpos($output, "n");
    }
    if ($pos !== false) {
      $output = $this->enclosure . substr($output, 0, $pos) . $this->enclosure . substr($output, $pos);
    }
  }
  if ($to_encoding) {
    $output = mb_convert_encoding($output, $to_encoding, $from_encoding);
  }
  $this->clear();
  return $this->output($output);
}

内容类型错误,它应该是text/csv ,而不是application/vnd.ms-excel

来源:维基百科


这是因为你的内容类型。

你应该改变:Content-Type:application / vnd.ms-excel

尝试这个:

<?php

header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
...

试试这个,它应该在所有浏览器上强制下载。

    header ("Content-type: octet/stream");
    header ("Content-disposition: attachment; filename="".$file."";");
    header("Content-Length: ".filesize($file));
    readfile($file);
链接地址: http://www.djcxy.com/p/95091.html

上一篇: Chrome is misinterpreting a file attachment as a document

下一篇: " to the downloaded CSV file