简体   繁体   English

HTML5 / Javascript代码可以从页面浏览器下载图像?

[英]HTML5/Javascript code to download a image from the page browser?

I need javascript code to download all images in a web page. 我需要javascript代码才能下载网页中的所有图像。 Is there a way to do it ? 有办法吗?

When the browser loads the page, there might be some images in that page, how to list all of them and download? 当浏览器加载页面时,该页面中可能会有一些图像,如何列出所有图像并下载? I mean this should not involve sending request to server. 我的意思是这不应该涉及将请求发送到服务器。 As, the browser has loaded the images, I need to download the images from the browser not the server. 由于浏览器已加载图像,因此我需要从浏览器而非服务器下载图像。 How can it be done? 如何做呢?

You can get all images and draw it on canvas and then use getImageData to get content. 您可以获取所有图像并将其绘制在画布上,然后使用getImageData获取内容。 See this link 看到这个链接

You can get a list of all the urls or iterate through the images like this 您可以获取所有URL的列表,或像这样遍历图像

var images = document.getElementsByTagName("img");

var urls = [];

Array.prototype.forEach.call(images, function (image) {
    if (image.src.length > 0) {
        urls.push(image.src);
    }
});

console.log(urls);

A live example is available on jsfiddle jsfiddle上有一个实时示例

It is also possible to save images to disk, see t his example , but not when they come from another domain. 也可以将图像保存到磁盘,请参见示例 ,但是当图像来自另一个域时则不能。

Here is an updated fiddle that shows the cross domain security error. 这是更新的小提琴 ,显示了跨域安全性错误。

Same Origin Policy 同源政策
Same-origin policy 同源政策

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM