【问题标题】:content-type causes chrome to call function twicecontent-type 导致 chrome 调用函数两次
【发布时间】:2013-04-18 08:44:01
【问题描述】:

以下代码最特殊的问题。它会向浏览器返回一个 pdf 报告。

function cart_aspdf() {
    trace('cart_aspdf_in');
    $file = 'order_WS000250.pdf';
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $file . '"');
    $file =  APPPATH.'pdfcache/'.$file;
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($file));
    header('Accept-Ranges: bytes');
    trace('cart_aspdf_readfile');
    @readfile($file);
    trace('cart_aspdf_out');
}

opera,firefox,ie,safari中的trace输出如你所料:

  1. cart_aspdf_in
  2. cart_aspdf_readfile
  3. cart_aspdf_out

但是 chrome 的跟踪显示以下内容,这似乎表明该函数至少被调用了两次,如果不是三次的话。为什么会这样?

  1. cart_aspdf_in
  2. cart_aspdf_readfile
  3. cart_aspdf_out
  4. cart_aspdf_in
  5. cart_aspdf_readfile
  6. cart_aspdf_in
  7. cart_aspdf_readfile
  8. cart_aspdf_out

如果我省略了 content-type 行,但 chrome 会显示没有用的原始 pdf 数据,则不会出现问题

【问题讨论】:

标签: php google-chrome pdf content-type


【解决方案1】:

我遇到了同样的问题。

header('Content-Disposition: inline;');

无论出于何种原因,当内容处置是内联时,它会调用该页面两次。

这让我在尝试使用引荐来源网址时遇到问题,因为第二次调用未传递引荐来源网址数据。

使用

header('Content-Disposition: attachment;');

只运行一次,但不会显示在浏览器的 PDF 查看器中。它将改为下载文件。

我认为这需要发布在 chrome 的 bugtracker 上。这很烦人,而且对于流式传输文件会浪费带宽。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2017-02-23
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多