content-type会导致chrome调用两次函数 [英] content-type causes chrome to call function twice

查看:176
本文介绍了content-type会导致chrome调用两次函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码最具特色的问题。它返回一个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 /'.$文件;
header('Content-Transfer-Encoding:binary');
header('Content-Length:'。filesize($ file));
头('Accept-Ranges:bytes');
trace('cart_aspdf_readfile');
@readfile($ file);
trace('cart_aspdf_out');
}

opera中的trace输出,即safari,就像您期望的那样:



  1. cart_aspdf_in

  2. cart_aspdf_readfile
  3. cart_aspdf_out


但是Chrome的跟踪显示以下内容,似乎表明函数被调用至少两次,如果不是三次。为什么要这么做?



  1. cart_aspdf_in

  2. cart_aspdf_readfile li>
  3. cart_aspdf_out

  4. cart_aspdf_in

  5. cart_aspdf_readfile
  6. cart_aspdf_in

  7. cart_aspdf_readfile
  8. cart_aspdf_out


如果我省略了内容类型行,但chrome显示没有用的原始pdf数据,则不会发生此问题。 解决方案

我遇到了同样的问题。

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

无论什么原因,当content-disposition内联时,它会调用页面两次。



这给了我试图使用引用者的问题,因为第二个调用不会传递引用数据。

使用



>

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

只运行一次,但不会显示在浏览器PDF阅读器内。它会下载文件。



我认为这需要发布在chrome的bug追踪器上。这是非常烦人的,并为流文件带宽浪费。

Most peculiar problem with following code. It returns a pdf report to the browser.

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');
}

The trace output in opera,firefox,ie,safari is as you would expect:

  1. cart_aspdf_in
  2. cart_aspdf_readfile
  3. cart_aspdf_out

BUT the trace for chrome shows the following which seems to indicate that the function is being called at least twice if not three times. Why should this be so?

  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

The problem does not occur if I omit the content-type line but then chrome shows the raw pdf data which is no use

解决方案

I ran into the same problem.

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

For whatever reason, when the content-disposition is inline it calls the page twice.

This was giving me problems trying to use the referrer because the second call does not pass referrer data.

using

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

only runs once, but will not display inside the browsers PDF viewer. It will instead download the file.

I think this needs to be posted on chrome's bugtracker. This is quite annoying and for streaming files a bandwidth waste.

这篇关于content-type会导致chrome调用两次函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆