javascript 防止 idm 使用 pdf.js 自动下载 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29954361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent PDF auto download by idm using pdf.js
提问by user3003810
I'm using PDF.Jsto embed PDF file for preview, and I removed the script of download and open files from the viewer.js
, but when I test the page and PDF file try to show, the Internet Download Manager download it and abort the preview .. after search I found that using object
instead of iframe
may solve the problem, but it didn't work the pdf viewer appeared white, what can I do to prevent auto download ? or using another way (Plugin) to show PDF file content.
我使用PDF.Js为预览嵌入PDF文件,我删除从下载和打开文件的脚本viewer.js
,但是当我测试页面和PDF文件试图表明,互联网下载管理器下载它,并中止预览..搜索后我发现使用object
而不是iframe
可以解决问题,但它不起作用pdf查看器出现白色,我该怎么做才能防止自动下载?或使用另一种方式(插件)来显示 PDF 文件内容。
<iframe
class="pdf"
webkitallowfullscreen=""
mozallowfullscreen=""
allowfullscreen=""
frameborder="no"
width="'.$width.'"
height="'.$height.'"
src="'.$baseurl.'/assets/pdf/web/viewer.html?file='.urlencode($pdf_url).'"
data-src="'.$pdf_url.'">
'.$pdf_url.'
</iframe>
采纳答案by ProllyGeek
This is not something related to developing issue , this is something related to user specific environement.
这与开发问题无关,这与用户特定的环境有关。
The Issue :
问题 :
Using IDM,any URL that ends with a media extension (e.g *.JPG , *.PNG , *.MP4 , *.WMV , *.PDF ..etc ) will be downloaded automatically , However on the other hand if the user doesnt have IDMinstalled , the file will be viewed immediately in browser window.
使用IDM,任何以媒体扩展名(例如 *.JPG、*.PNG、*.MP4、*.WMV、*.PDF ..etc)结尾的 URL 都将被自动下载,但是另一方面,如果用户没有有无IDM安装,该文件将立即在浏览器窗口中查看。
Possible Solutions :
可能的解决方案 :
- Remove PDF extension Handler from IDM , to prevent auto download, and i think the image explains it very well.
- 从 IDM 中删除 PDF 扩展名处理程序,以防止自动下载,我认为图像很好地解释了它。
- Modify response header for your PDF link to force your browser to view pdf inside its view , please consider that each browser may handle the response differently , more details about this method can be found here.
- 修改您的 PDF 链接的响应标题以强制您的浏览器在其视图中查看 pdf,请考虑每个浏览器可能会以不同的方式处理响应,有关此方法的更多详细信息可以在此处找到。
Final Note:
最后说明:
As a developer you shouldnt handle each user specific environment , we suppose that when user installs a specific app to handle generic files , then it is his/her role to handle that application , and not the developer role , cause if you follow this algorithm you jump inside infinite loop handling different users specific setup.
作为开发人员,您不应该处理每个用户的特定环境,我们假设当用户安装特定应用程序来处理通用文件时,处理该应用程序是他/她的角色,而不是开发人员角色,因为如果您遵循此算法,跳入无限循环处理不同用户的特定设置。
回答by Arif
Just remove .pdf
Extension from the main file.
IDM cannot detect what kind of file it is. but the browser will handle its native way.
只需.pdf
从主文件中删除扩展名。IDM 无法检测它是什么类型的文件。但浏览器会处理它的原生方式。
回答by Halayem Anis
try with this
试试这个
<embed src="'.$baseurl.'/assets/pdf/web/viewer.html?file='.urlencode($pdf_url).'" type="text/html" >
回答by dong kuk Park
Judging by your comments, I'd say your uri may be incorrect.
You can try replacing the uri with any pdf file online to see if the rest of the code is okay.
从您的评论来看,我会说您的 uri 可能不正确。
您可以尝试将 uri 替换为任何在线 pdf 文件,以查看其余代码是否正常。
If you just want to embed a PDF object, you could try using PDFobject.js.
https://github.com/pipwerks/PDFObject
http://pdfobject.com/instructions.php
如果您只想嵌入 PDF 对象,可以尝试使用 PDFobject.js。
https://github.com/pipwerks/PDFObject
http://pdfobject.com/instructions.php
HTML
HTML
<div id="my_pdf_object"
class="pdf"
webkitallowfullscreen=""
mozallowfullscreen=""
allowfullscreen=""
frameborder="no"
width="'.$width.'"
height="'.$height.'"
>
It appears you don't have Adobe Reader or PDF support in this web browser.
<a href="'.$baseurl.'/assets/pdf/web/viewer.html file='.urlencode($pdf_url).'">
Click here to download the PDF</a>
</div>
JavaScript
JavaScript
<script type="text/javascript">
//loads pdf files for resume
window.onload = function (){
var success = new PDFObject({
url: "'.$baseurl.'/assets/pdf/web/viewer.html?file='.urlencode($pdf_url).'"
}).embed("my_pdf_object");
};
</script>
回答by Sen Sokha
It is possible to prevent download automatically from idm (PdfJs). IDM find extention pdf file so we need to convert pdf file to base-64 and then read base64 of pdf into node DOM src:
可以防止从 idm (PdfJs) 自动下载。IDM找到扩展pdf文件,所以我们需要将pdf文件转换为base-64,然后将pdf的base64读入节点DOM src:
1. Convert your file pdf to base64
1. 将您的文件 pdf 转换为 base64
document.getElementById('myfiles').addEventListener('change', function(event){
var input = document.getElementById("myfiles");
var fReader = new FileReader();
fReader.readAsDataURL(input.files[0]);
fReader.onloadend = function(event){
document.getElementById("base64").innerHTML = event.target.result;
console.log(event.target.result);
}
});
<input type="file" name="files" id="myfiles" value=""><br>
<textarea id="base64" cols="50"></textarea>
2. get all string base64 put to txt file
2.获取所有字符串base64放入txt文件
filename.txt
文件名.txt
3. read base64 from file txt and past into iframe or with viewer.js (PDF JS)
3.从文件txt中读取base64并过去进入iframe或使用viewer.js(PDF JS)
-iframe should
- iframe 应该
<iframe scr="data:application/pdf,base64.....">prevent download from idm</iframe>
-with PDF viewer
-带有PDF查看器
var xhr = new XMLHttpRequest();
console.log(file);
xhr.open('GET', folderFiles+filename+".txt", true);
xhr.responseType = 'text';
xhr.onload = function(e) {
if (this.status == 200) {
PDFViewerApplication.open(this.response);
}
};
So, IDM won't force download pdf file automatically because it cannot find specific file. link : https://github.com/sokhasen/ViewerPDF.git
因此,IDM 不会因为找不到特定文件而自动强制下载 pdf 文件。链接:https: //github.com/sokhasen/ViewerPDF.git