简体   繁体   English

将图像从网站下载到桌面

[英]downloading image from website to desktop

Can someone please explain to me. 有人可以向我解释。 Why my this code is not working? 为什么我的这段代码无法正常工作? I have changed my code over and over and its hard to explain what is not working. 我一遍又一遍地更改了代码,很难解释什么不起作用。 So i finally put it in my website so I can show you what is going on. 所以我最终将其放在我的网站上,以便向您展示发生了什么。 Here is where i found the code: convert div to single image with canvas-javascript css I use this an example because I'm trying to do the same thing. 我在这里找到了代码: 使用canvas-javascript css将div转换为单个图像我使用了一个示例,因为我试图做同样的事情。 I changed the images so you can see on my site https://torcdesign.com/mom.php 我更改了图像,以便您可以在我的网站上看到https://torcdesign.com/mom.php

 var download = document.getElementById("download"), result = document.getElementById("result"); function renderContent() { html2canvas(document.getElementById("content"), { allowTaint: true }).then(function(canvas) { result.appendChild(canvas); download.style.display = "inline"; }); } function downloadImage() { download.href = result.children[0].toDataURL("image/png"); } document.getElementById("button").onclick = renderContent; download.onclick = downloadImage 
 <style> #content { position: absolute; width: 300px; height: 200px; border: 5px solid red; overflow: hidden; } #img1 { width: 300px; height: 200px; position: absolute; z-index: 5; } #img2 { position: absolute; z-index: 6; width: 150px; height: 190px; } #img3 { position: absolute; z-index: 7; width: 200px; height: 100px; } #download { display: none; } </style> 
 <script src="https://rawgit.com/niklasvh/html2canvas/master/dist/html2canvas.min.js"></script> <div id="content"> <img id="img1" src="https://torcdesign.com/shirts/brown.jpg"> <img id="img2" src="https://torcdesign.com/shirts/kiwi.jpg"> <img id="img3" src="https://torcdesign.com/shirts/lswhite.jpg"> </div> <br><br><br><br><br><br><br><br><br><br><br><br> <input id="button" type="button" value="convert div to image"><br> <h3>result:</h3> <div id="result"></div> <a id="download" download="my_image.png" href="#">Download image</a> 

After some digging, it looks like the issue is that your canvas is tainted, and you cannot export a tainted canvas. 经过一番挖掘后,问题似乎出在您的画布已被污染,而您无法导出已污染的画布。 To make sure you canvas is not tainted, you cannot use cross-origin images. 为了确保您的画布没有污染,您不能使用跨域图像。 For example, I tried using a base-64 encoded image instead, and that one worked perfectly fine. 例如,我尝试使用base-64编码的图像代替,并且该图像效果很好。

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

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