6

I want to download a image file (jpeg image) to the user files system when user selects a photo and clicks a button. So far I searched and found this link and also this

I saw in one blog that downloadify when used with jszip can enable this feature but it didn't specify any farther on this. Does any one know how to download the images. I have the links to the images and i just want the user to download it on his system rather than again query the server.

Can anyone provide me with an example please.

3
  • 1
    What have you tried? And here is an example. Commented Sep 2, 2012 at 18:23
  • yeah i already saw that ...I mentioned clearly in my question that it is for image file not text file Commented Sep 2, 2012 at 18:24
  • I have tried jszip but couldn't get it in the correct format...i provided the link in the question Commented Sep 2, 2012 at 18:25

4 Answers 4

7

Use the HTML5 download attribute.

As a choice you can set filename as an attribute value.

<a href="/images/image-name.jpg" download="new-image-name.jpg"> 
Sign up to request clarification or add additional context in comments.

1 Comment

When the download link is from a website like pixabay it opens it in the same tab and returns 403 unauthorised as it need authentication
5

You can load the image in an canvas element get the data url of the canvas and open a new window with the data url as source. Take a look at this example: https://gist.github.com/1875132

3 Comments

thank you for the comment..i will try it out :) ...by the way I am using a normal img tag will this code work for img tag or does it ony work for canvas tag ?
The problem is that you cant get the data url for an image tag. Thats way you need to convert it to a canvas first.
can u plz explain what data url is ...I am kinda noob to this stuff :( Actually I am making a website which is integrated with facebook and facebook passes me the link to the image(jpeg) by using graph api.The image retrieval code is here in this link stackoverflow.com/questions/12234853/…
2

Needed for a project to make a custom context(right-click) menu with a custom download image button.

Here is an answer using pure javascript:

const imageElement = document.getElementById("YourImage").getAttribute("src");

const saveImage = (downloadUrl: string) => {
    const downloadImage = document.createElement("a");
    document.body.appendChild(downloadImage);
    downloadImage.setAttribute("download", "image");
    downloadImage.href = downloadUrl;
    downloadImage.click();
    downloadImage.remove();
};

saveImage(imageElement)

Comments

1

Finally I did it. For anyone who may need this in the future here is how I did it using jquery

jQuery.ajax({

    url: 'http://localhost:8080/yourwebsite/servlet?img=' + document.getElementById(id).alt,
    //data: myData,
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
   // success: function() { alert("Success"); },
   // error: function() { alert('Failed!'); },
   // beforeSend: setHeader
});

this I had to do come across the problem of cross domain http requests which are usually blocked by most websites unless you follow some lengthy process. So, I made it simpler and called a get method in my servlet and passed it the url of the image from which it downloaded the image.This is much easier to do and even easier then this if you are on the same domain but that didn't meet my requirements and this code worked for me :)

3 Comments

Will your code work in my condition ?
Id is image id? Or what
@Mahmut215 it's just the url for the image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.