Force <a download /> to download image instead of opening url link to image

net force
what is force in science
force formula
force synonym
force examples
types of force
example of force
simple definition of force

<a download='file' href="https://tinyjpg.com/images/social/website.jpg">
  Download
</a>

You may be bitten by the fact that Firefox and Chrome 65+ only support same-origin download links, probably as a security measure.

Source: https://caniuse.com/#feat=download (see "Known issues" tab)

The Web Hypertext Application Technology Working Group (WHATWG) also recommends that in cross-origin scenarios (as in your example), the web server that is hosting the image/file in question needs to send a Content-Disposition HTTP header for download= to be honored.

Source: https://html.spec.whatwg.org/multipage/links.html#downloading-resources


In short:

You can only use <a download='...'></a> to force download of an image/file, if:

  1. the server that hosts the image/file also says it should be downloaded,or
  2. the image/file comes from the same domain.

Force, coercion or compulsion, especially with the use or threat of violence. Contact force is defined as the force exerted when two physical objects come in direct contact with each other. Other forces, such as gravitation and electromagnetic forces, can exert themselves even across the empty vacuum of space.

Maybe you have solved in the meanwhile, but since you are hosting the files on S3 (see comments on Peter B's answer), you need to add a signature to the files url and set the ResponseContentType to binary/octet-stream by using the aws sdk. I am using Node so it becomes:

const promises = array.map((item) => {
        const promise = s3.getSignedUrlPromise('getObject', {
            Bucket: process.env.S3_BUCKET,
            Key: key, //filename
            Expires: 604800, //time to expire in seconds (optional)
            ResponseContentType: 'binary/octet-stream' 
        });

        return promise;
    });

    const links = await Promise.all(promises);

I hope this helps.

Force, make a way through or into by physical strength; break open by force. Synonyms for force at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for force.

setTimeout(function() {
  
   url = 'https://media.sproutsocial.com/uploads/2017/02/10x-featured-social-media-image-size.png';
   // downloadFile(url); // UNCOMMENT THIS LINE TO MAKE IT WORK
  
}, 2000);

// Source: http://pixelscommander.com/en/javascript/javascript-file-download-ignore-content-type/
window.downloadFile = function (sUrl) {

    //iOS devices do not support downloading. We have to inform user about this.
    if (/(iP)/g.test(navigator.userAgent)) {
       //alert('Your device does not support files downloading. Please try again in desktop browser.');
       window.open(sUrl, '_blank');
       return false;
    }

    //If in Chrome or Safari - download via virtual link click
    if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
        //Creating new link node.
        var link = document.createElement('a');
        link.href = sUrl;
        link.setAttribute('target','_blank');

        if (link.download !== undefined) {
            //Set HTML5 download attribute. This will prevent file from opening if supported.
            var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
            link.download = fileName;
        }

        //Dispatching click event.
        if (document.createEvent) {
            var e = document.createEvent('MouseEvents');
            e.initEvent('click', true, true);
            link.dispatchEvent(e);
            return true;
        }
    }

    // Force file download (whether supported by server).
    if (sUrl.indexOf('?') === -1) {
        sUrl += '?download';
    }

    window.open(sUrl, '_blank');
    return true;
}

window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;

The Meaning of Force, Force definition is - strength or energy exerted or brought to bear : cause of motion or change : active power. How to use force in a sentence. Synonym� Personalize every experience along the customer journey with the Customer 360. Unify marketing, sales, service, commerce, and IT on the world's #1 CRM.

Force.com Platform, A force is a push or pull upon an object resulting from the object's interaction with another object. Whenever there is an interaction between two objects, there is a force upon each of the objects. When the interaction ceases, the two objects no longer experience the force. Force definition, physical power or strength possessed by a living being: He used all his force in opening the window. See more.

force | Definition & Formula, Force.com is part of Salesforce Customer 360 Platform. Learn about the fastest way to build custom employee-facing mobile enterprise apps. The normal force acts in a direction normal to the surface interaction between objects. Friction is a force that opposes motion on surfaces. Other examples of non-fundamental forces include the elastic force, tension, and frame-dependent forces, such as centrifugal force and the Coriolis force.

Facing Our Risk of Cancer Empowered, Force, in mechanics, any action that tends to maintain or alter the motion of a body or to distort it. The concept of force is commonly explained in terms of Isaac � The world's most advanced graphics cards, gaming solutions, and gaming technology - from NVIDIA GeForce. Download drivers, automate your optimal playable settings with GeForce Experience.

Comments
  • Don't the code you have there work?
  • No as you can see if you run it, it opens a new page with the image instead of downloading it
  • Your code there should work. However, you've renamed your download to 'file', if it's a jpg or an image you may want to add that file extension onto it.
  • In addition (or in replacement) to the HTML5's <a download attribute already mentioned, the browser's download to disk behavior can also be triggered by the following http response header: Content-Disposition: attachment; filename=ProposedFileName.txt; This was the way to do before HTML5 (and still works with browsers supporting HTML5). Ref: stackoverflow.com/questions/2793751/…
  • I tried and it works just fine (with another file)... Can you use a snippet?
  • Thanks for the answer @peter! Our backend is hosting the files on S3, is there a setting there for Content-Disposition to allow download, or can it be defined in our API?
  • @gnifrus should be set from AWS
  • Please explain why this code is a good solution for the OP, code only answers are frowned upon by SO