javascript 如何限制用户下载或保存pdf文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14664923/
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
How to restrict user from downloading or saving pdf file?
提问by Ashwin
Possible Duplicate:
Prevent PDF file from downloading and printing
可能的重复:
防止下载和打印 PDF 文件
In my current symfony project (in admin section) I uploaded pdf file and open it by using JavaScript, but I want to restrict user to download or save for that pdf?
在我当前的 symfony 项目(在管理部分)中,我上传了 pdf 文件并使用 JavaScript 打开它,但我想限制用户下载或保存该 pdf?
So is it possible to me to restrict user to download or save pdf?
那么我可以限制用户下载或保存 pdf 吗?
I means user can only view content of that pdf, i want to disable download or save options of all browsers for that pdf.
我的意思是用户只能查看该 pdf 的内容,我想禁用该 pdf 的所有浏览器的下载或保存选项。
pdf file is open from server only.
pdf 文件仅从服务器打开。
Action File Code:
操作文件代码:
echo "<span onclick=\"clickme('$pdf_name')\";>view</span>";
Success File Code:
成功文件代码:
<script type="text/javascript">
function clickme(file_name){
window.open(file_name);
}
</script>
Thank you in advanced.
先谢谢了。
回答by David
It's practically impossible to prevent people from downloading anything, much less PDF's. I'm afraid you're gonna have to go with alternatives, such as only showing a preview of the document or using watermarks.
阻止人们下载任何东西几乎是不可能的,更不用说 PDF 了。恐怕您将不得不采用替代方案,例如仅显示文档预览或使用水印。
回答by JohnFx
The simple fact is that if the user is viewing the PDF file, they have already downloaded it to a temporary directory on the computer.
一个简单的事实是,如果用户正在查看 PDF 文件,他们已经将其下载到计算机上的临时目录中。
I see questions like this all the time (mostly for GIF/JPEG), but you have to understand that a solution to prevent this is completely impossible because you have already lost the game when you sent the PDF down to the user's machine.
我一直看到这样的问题(主要针对 GIF/JPEG),但您必须明白,完全不可能防止这种情况发生,因为当您将 PDF 发送到用户的机器时,您已经输了。
The best you can hope for is to make it difficult on the user to do this, but with enough effort there is no way to stop them. For example, how would you prevent them from taking screenshots of the image, or just grabbing it from the browser cache directly?
您所能希望的最好结果是让用户难以执行此操作,但只要付出足够的努力,就无法阻止他们。例如,您将如何防止他们截取图像的屏幕截图,或者直接从浏览器缓存中抓取图像?
You might have luck thwarting non-savvy users by setting the security flags in the PDF fileitself.
回答by mohammad mohsenipur
yes It Can be by Force browser in file Header
是 可以通过文件头中的强制浏览器
it force browser To download Instead view it is down By Htaccess and You Can Do By PHP Too
它强制浏览器下载而不是通过 Htaccess 查看它已关闭,您也可以通过 PHP 执行
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
see below link I hope Help You
看下面的链接希望对你有帮助