简体   繁体   English

使用Javascript通过Firefox下载图像?

[英]Download Image through Firefox Using Javascript?

I'm working on an extension for Firefox right now, written in javascript. 我正在使用javascript编写Firefox扩展。 Essentially when hovering over the thumbnail of an image (which when clicked on takes you to a webpage with the full image in it) the extension finds the URL of the full image and attempts to download it by way of a key press. 本质上,当将鼠标悬停在图像的缩略图上时(单击该缩略图会将您带到其中包含完整图像的网页),该扩展程序会找到完整图像的URL并尝试通过按键下载。 ("D" in this instance.) The file's I'll be working with are typically .jpgs and .pngs, however .swfs and .mp3s may also be present. (在这种情况下为“ D”。)我要使用的文件通常是.jpgs和.pngs,但是也可能存在.swfs和.mp3s。 At this point I'm just looking to get the first two filetypes working. 现在,我只是想让前两种文件类型正常工作。

The code I'm using right now is simply 我现在使用的代码很简单

function ExtendedDownload(image_src) {
    var a = document.createElement('a');
    document.body.appendChild(a);
    a.download = image_src.split('/').pop().split('?')[0];
    a.href = image_src;
    a.click();
}
  1. image_src is the URL to the image. image_src是图像的URL。
  2. a.download is the filename of the image which is parsed from the full URL. a.download是从完整URL解析的图像文件名。

Earlier on in the javascript file it waits for the keypress when it sees that a valid thumbnail is being hovered over by the cursor. 早先在javascript文件中,当看到光标将有效缩略图悬停时,它会等待按键。 This part works perfectly fine. 这部分工作正常。 The downloading is what I'm having trouble with. 下载是我遇到的麻烦。

When loaded into chrome, this extension works perfectly as intended. 当加载到chrome中时,此扩展程序可以按预期完美运行。 However in Firefox (v49.0.2) it simply displays the image (akin to pasting the image's link in the address bar and pressing enter.) This is not the desired effect. 但是,在Firefox(v49.0.2)中,它仅显示图像(类似于将图像的链接粘贴到地址栏中,然后按Enter。)这不是理想的效果。

One solution I tested was converting the image to base64 through javascript and attempting to download that, which works in theory, and I've tested it will small base64 converted .pngs.. However the files I'm attempting to download result in base64 strings that are more than a megabyte in size and send the browser hanging so this isn't a valid option. 我测试过的一种解决方案是通过javascript将图像转换为base64并尝试下载,这在理论上是可行的,并且我已经测试过它将base64转换后的.png小文件。但是,我尝试下载的文件以base64字符串形式大小超过1兆字节,并导致浏览器挂起,因此这不是有效的选择。

Every solution that I've run into on stackexchange for similar issues puts me back at this spot however, where "downloading" the image simply opens it in the browser. 我在stackexchange上遇到的类似问题的所有解决方案都使我回到这个位置,在这里“下载”图像只是在浏览器中打开它。

Any suggestions would be appreciated. 任何建议,将不胜感激。

Edit: The issue I'm having is definitely based on the fact that the webpage I'm using this extension on and the webpage the image is on are on different domains. 编辑:我遇到的问题绝对是基于以下事实:我使用此扩展程序的网页和图像所在的网页位于不同的域中。 @Mayank Pandeyz's solution has convinced me of this as when I try donwloading and image on the same domain as the page itself it works as intended. @Mayank Pandeyz的解决方案使我相信了这一点,因为当我尝试在与页面本身相同的域上进行下载和映像时,它可以按预期工作。 What are my potentials workaround for something like this? 对于这种事情,我的潜在解决方法是什么?

Try this: 尝试这个:

var a = document.createElement('a');
a.href = "/favicon.png";
a.download = "favicon.png";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

Demo Fiddle 演示小提琴

you could do it like this in jquery. 您可以在jquery中这样做。

where you give choice to user whether to download or not 您可以选择是否下载的位置

   <a href="yourPath" class="download">download</a>


$('.download').click(function(){
           var url = $(this).attr('href');
           $(this).attr("target", "_blank");
            window.open(url,'Download');  
       });

here is a fiddle: https://jsfiddle.net/s1ojfbrt/2/ 这是一个小提琴: https : //jsfiddle.net/s1ojfbrt/2/

Your server shall have an option to supply the image with the following response headers: 您的服务器应可以选择为图像提供以下响应头:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="picture.png"

In this case browser will show Save As... dialog. 在这种情况下,浏览器将显示“另存为...”对话框。

You can have special url/script on the server for that. 您可以为此在服务器上使用特殊的url /脚本。

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

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