javascript 如何防止用户下载或保存图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4506124/
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 prevent user from downloading or saving an image?
提问by Nik
Well, i have a web page where i am displaying few images. But my problem is that i don't want user to download or save those images. I can apply watermark to those images but that is alternative option. I can also disable right click but what if user saves the page?
好吧,我有一个网页,我在其中显示了一些图像。但我的问题是我不希望用户下载或保存这些图像。我可以对这些图像应用水印,但这是另一种选择。我也可以禁用右键单击但如果用户保存页面怎么办?
Any alternative solution will also do?
任何替代解决方案也可以吗?
Hope this question is clear?
希望这个问题很清楚?
回答by deceze
If the user can see the image, it's already on his computer. Saving it to a file or copying it to the clipboard is trivial and cannot be disabled in any reliable way.
如果用户可以看到图像,则它已经在他的计算机上。将它保存到文件或复制到剪贴板是微不足道的,不能以任何可靠的方式禁用。
If you want to keep control over the image, don't put it on the internet.
Watermarking is the best you can do.
如果您想保持对图像的控制,请不要将其放在互联网上。
水印是您能做的最好的事情。
回答by HoLyVieR
If the user can view the image there is no way you can stop him to save it, because at that point you have already sent him to file.
如果用户可以查看图像,则您无法阻止他保存图像,因为此时您已经将他发送到文件中。
You can implement some pseudo-protection code such as anti Right-Click to "block" the saving, but overall it's just annoying to user of your website and it's always possible to go around it.
您可以实现一些伪保护代码,例如反右键单击来“阻止”保存,但总的来说,这对您网站的用户来说很烦人,而且总是可以绕过它。
If you don't want your image to be propagated, watermark them or don't publish them.
如果您不想传播您的图像,请为它们添加水印或不要发布它们。
回答by Evan Lévesque
if you are using Apache server, you can disable accessing the image through absolute url
the images can be access only with relative url with this htaccess code :
如果您使用的是 Apache 服务器,则可以禁用通过绝对 url 访问图像,
这些图像只能使用具有以下 htaccess 代码的相对 url 访问:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
in addition, disable the right click context using JavaScript, and add a watermark to protect the copy rights
this will reduce the chance of saving the images
另外,使用JavaScript禁用右键单击上下文,并添加水印以保护版权,
这将减少保存图像的机会
回答by mario
Porn website, eh?
网站,嗯?
Well, you cannot prevent it. You can screw Windows and IE users with a Javascript trick, but as said before, that's easy to disable. You can't stop users from saving of the whole page either. People can also just make screenshot, cut and resave. All resources available via URLs are locateable and can be downloaded.
好吧,你无法阻止它。您可以使用 Javascript 技巧来欺骗 Windows 和 IE 用户,但如前所述,这很容易禁用。您也无法阻止用户保存整个页面。人们也可以只是截图,剪切和重新保存。通过 URL 提供的所有资源都是可定位的并且可以下载。
There's one option to frustrate downloading of images though: automatically slice it. If the image is 800x600 pixel, you could split it into a hundred(?) 80x60 smaller graphics. That would make reassembling slightly more difficult. But also turn the rendering slightly unrealiable.
不过,有一种方法可以阻止图像的下载:自动对其进行切片。如果图像是 800x600 像素,您可以将其拆分为一百个(?)80x60 较小的图形。这将使重新组装稍微困难一些。但也使渲染稍微不真实。
回答by Victor Zamanian
There always seem to be ways to circumvent this, so you shouldn't even bother. I have never been unable to break this type of blocking functionality. If applicable, it's probably better to watermark your images with your name, date and a copyright symbol or (C).
似乎总是有办法绕过这个,所以你甚至不应该打扰。我从未无法打破这种类型的阻塞功能。如果适用,最好使用您的姓名、日期和版权符号或 (C) 为您的图像添加水印。
回答by mhd
There's no absolute solution to prevent user from downloading your image. If you publish image in the web,just assume that somebody can copy it. Whatever your solution, in the end user can take a screen shot anyway.
没有绝对的解决方案可以阻止用户下载您的图像。如果您在网络上发布图像,只需假设有人可以复制它。无论您采用何种解决方案,最终用户都可以进行屏幕截图。
回答by Brooks Moses
I've seen a few web pages that put a transparent single-pixel .gif as an overlay across the "main" image that they're displaying, so that when you right-click and save you only save the single-pixel image. I'm not sure of the exact HTML for that, but there are a few obvious ways to do it.
我见过一些网页将透明的单像素 .gif 作为覆盖在它们显示的“主”图像上,这样当您右键单击并保存时,您只能保存单像素图像。我不确定具体的 HTML,但有一些明显的方法可以做到这一点。
(This, like the other tricks, is not particularly difficult to circumvent, but it does at least add an annoyance factor that will block most people.)
(这和其他技巧一样,不是特别难规避,但它至少增加了一个会阻止大多数人的烦恼因素。)
回答by The Scrum Meister
As mention in all the answers, there is no way to prevent users from saving the image, however you can try to make it hard for a user to do it.
正如所有答案中提到的,没有办法阻止用户保存图像,但是您可以尝试让用户难以保存图像。
although this is not good performance, you can split the image into many small pieces, making it harder (but not at all impossible) to save them and join them.
虽然这不是很好的性能,但您可以将图像分成许多小块,从而更难(但并非完全不可能)保存和加入它们。
回答by Abel ANEIROS
Prevent users from downloading images it's a waste of time because even if they cannot download the image, they always could do an screenshot:-(
阻止用户下载图片是浪费时间,因为即使他们无法下载图片,他们也总是可以做一个截图:-(
回答by rajmohan
There are so many ways to download images from website, so whatever you do at the end of the day someone is going to download it.
有很多方法可以从网站下载图像,所以无论你在一天结束时做什么,都会有人下载它。

