Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to download an image file in my application from other domain image path. if i try to download same domain image path then it's work fine but for other domain path the image file open in browser and no option found to download this image except right click and save image. i want to direct download cross domain image without open in browser.

here is my code :-

<a href="http://localhost:51255/images/test.png" target="_blank" download="1500C.png"> download image</a>


could any one suggest me the solution.

What I have tried:

i am trying it using javascript but getting error cross domain related errors:-

function forceDownload(url, fileName) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.responseType = "blob";
    xhr.onload = function () {
        var urlCreator = window.URL || window.webkitURL;
        var imageUrl = urlCreator.createObjectURL(this.response);
        var tag = document.createElement('a');
        tag.href = imageUrl;
        tag.download = fileName;
        document.body.appendChild(tag);
        tag.click();
        document.body.removeChild(tag);
    }
    xhr.send();
}
Posted
Updated 21-Jul-21 20:28pm

The Access-Control-Allow-Origin header is a response header, not a request header. It needs to be set by the site you're trying to download from. If you could override it with a request header, it would serve no purpose.
Cross-Origin Resource Sharing (CORS) - HTTP | MDN[^]

The download link won't work for cross-origin requests unless the other server sends a Content-Disposition: attachment header with the response. Again, this is for security reasons.
HTML Standard - 4.6.5 Downloading resources[^]

You're going to need to make the image request from your server, rather than from client-side code. You can then send it back to the client with the appropriate headers to make it download instead of opening in the browser.
 
Share this answer
 
This is a solution that works for me. It is JS and PHP.
First create anchor tag with href to the image you want to download. If you want you can set custom name to the save.as file (download attribute).

Then JS function fetchImageAndDownload. And then PHP part.
This code bassicaly tricks the browser into thinking that the file(image) is located on your server, as you temporarly save the file on your server.

HTML
<a href="url to the image" download="custom_name">Download</a>


JavaScript
function fetchImageAndDownload (e) {
    e.preventDefault(); // Prevent browser's default download stuff...

    const url = this.getAttribute("href");       // Anchor href 
    const downloadName = this.download;          // Anchor download name

    const img = document.createElement("img");   // Create in-memory image
    img.addEventListener("load", () => {
        const a = document.createElement("a");   // Create in-memory anchor
        a.href = img.src;                        // href toward your server-image
        a.download = downloadName;               // :)
        a.click();                               // Trigger click (download)
    });
    img.src = 'fetchImage?url='+ url;       // Request image from your server

}


PHP
fetchImage() {

    $url = $_GET["url"];                     // the image URL
    $info = getimagesize($url);              // get image data
    header("Content-type: ". $info['mime']); // act as image with right MIME type
    readfile($url);                          // read binary image data
    die();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900