简体   繁体   中英

(Javascript) How to force the download of an image using iframe?

My requirement is to select a bunch of images and download them all, all at the same time.

I'm using this library ( https://github.com/biesiad/multiDownload ) that works pretty well with other files than iamges. When it is image, it doesn't work.

My conclusions is that the browser perceives images differently from other binary files.

I've checked on Google Chrome and all the files that work using that plugin, they are cancelled (developer tools > network) but the images aren't.

Is there any way to tell browser that it's a binary like all the other files? Is there any way?

Many thanks


The function is implemented like that:

var methods = {
    _download: function (options) {
        var triggerDelay = (options && options.delay) || 100;
        var cleaningDelay = (options && options.cleaningDelay) || 1000;

        this.each(function (index, item) {
            methods._createIFrame(item, index * triggerDelay, cleaningDelay);
        });
        return this;
    },

    _createIFrame: function (item, triggerDelay, cleaningDelay) {
        setTimeout(function () {
            var frame = $('<iframe style="display: none;" ' +
                          'class="multi-download-frame"></iframe>');
            frame.attr('src', $(item).attr('href') || $(item).attr('src'));
            $(item).after(frame);
            setTimeout(function () { frame.remove(); }, cleaningDelay);
        }, triggerDelay);
    }
};

$.fn.multiDownload = function(options) {
    return methods._download.apply(this, arguments);
};

UPDATE: The README.md on multiDownload on Github says...

Important: All $('.my_links') elements must have defined "href" attribute. "href" must point to documents that generate proper HTML headers ("Content-Disposition: attachment; filename=my_filename").

The solution? What they said: send out the following header:

Content-disposition: attachment; filename="picture.jpg"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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