缓存的 base64 pdf 不会显示

base64 pdf cached won't show

我有一个对象标签,它显示来自 base64 字符串的 pdf,如下所示:

<object id="pdfViewer" data="data:application/pdf;base64,BASE64STRING" wmode="transparent" type="application/pdf" title="document" width="100%" height="800" internalinstanceid="60" style="height: 401px;"></object>

在某些计算机上,第一次加载文件时,它会按应有的方式显示。 然后是 "loaded from the cache"(在 chrome 网络检查器中)并且该对象不呈现任何内容。

如果我以隐身模式返回页面,它会再次运行(只是第一次)。如果我从浏览器中清除缓存也是如此。

有没有人知道可能导致此问题的原因?

谢谢!

好吧,我的回答有点晚了,因为我最近也 运行 遇到了同样的问题。

我想显示来自服务器的 pdf 文件。

由于包含 pdf 的文件夹受 htaccess 保护,我做了一个 ajax 请求,该请求将 return 文件内容编码为 base64。 Chrome 会缓存它,它只会在第一次呈现,而任何其他时间只会呈现空白......在 Safari 或 Firefox 上它工作得很好但我也必须为 Chrome 修复它。

所以,这是我所做的:

这是我的 ajax 文件的一部分,它创建了 pdf 和 "echoes" 的 Base64 编码内容:

<?php

// ....some -irrelevant to the case- code here

$output_doc = /path/to/myDoc.pdf;
$file = file_get_contents($output_doc);
//echo base64_encode($file); //here caching was working and problem existed
echo base64_encode($file."?".time()); //here I eliminated caching by adding a "time" parameter

?>

这就是我在 "object" 标签中呈现 pdf 文件内容的方式:

$.ajax({
    type:'post',
    url:'/ajax/file_get.ajax.php',
    data:'file=/path/to/myDoc.pdf',
    success:function(response){
        $('#pdf_object').attr('data','data:application/pdf;base64,'+response);
    }
});

最后,这是我的 html 中显示文件的部分:

<object type="application/pdf" id="pdf_object" style="width:100%; min-height:400px; height:100%;"></object>

希望对您(和其他人)有所帮助!