Download images with Javascript

Asked

Viewed 4,125 times

1

  • cool, what you’ve tried to do, already have some code, some test?

  • https://stackoverflow.com/questions/17311645/download-image-with-javascript

  • Well, I didn’t find any native javascript function for download.

  • @Diegoqueiroz there is no code that does this directly, but see Luiz Santos' answer.

3 answers

3

Taking advantage of the idea of the loop of Luiz Santos and the script of the brother of this post

for(var x = 1;x <= 25; x++ ){
if( x < 10)
var url = "0" + x + ".jpg";
else
var url = x + ".jpg";

var a = document.createElement('a');
a.href = "http:///the_gamer/the_gamer035-"+url;
a.download = "035-"+url;
a.click();

}

In Google Chrome Version 61.0.3163.100 is asked a question "Download multiple files" "Allow" "Block". The browser must be configured to download files automatically. Otherwise you will be waiting for confirmation of each download.

To automatically configure downloads on Google Chrome

1 - On your computer, open Google Chrome.

2 - In the upper right corner, click on tres seta Settings

3 - At the bottom of the page, click on Advanced

4 - In the "Downloads" section, adjust your download settings

The question link doesn’t seem to work http:///the_gamer/the_gamer035-01.jpg

  • http://uploader.centraldemangas.com.br/the_gamer/the_gamer035-01.jpg

  • I tested but did not download the photo but an html page. LOL

  • I think you made a mistake in the URL This link opens a page, not an image URL

  • When I load the URL I just sent in the browser the image appears. But when I put in the script only download the main page of the site. The URL is "http://uploader.centraldemangas.com.br/the_gamer/the_gamer035-01.jpg"

  • good @Leocaracciolo liked the idea.

  • your link redirects you to http://centraldemangas.etc.br/

  • @Diegoqueiroz pagina temporaria para vc testar http://kithomepage.com/sos/download-img-com-javascript.htm

  • It worked, I had already tested on another page and it had worked perfectly, only on that one I sent up that no funnel. But VLW.

  • @Diegoqueiroz because then, the people of this site implemented something not to funfar and leave you to see ships :)

  • Fuck, damn kkk. Vlw ai mano.

Show 5 more comments

2

Javascript for downloading an image is as follows:

var a = $("<a>")
    .attr("href", "http:///the_gamer/the_gamer035-01.jpg")
    .attr("download", "img.png")
    .appendTo("body");

a[0].click();

a.remove();

Knowing this just make a go going through the item number you want:

var url = "http:///the_gamer/the_gamer035-"

for(var x = 1;x <= 25; x++ ){
if( x < 10)
var url = url + "0" + x + ".jpg";
else
var url = url + x + ".jpg";

var a = $("<a>")
    .attr("href", "http:///the_gamer/the_gamer035-01.jpg")
    .attr("download", "img.png")
    .appendTo("body");

a[0].click();

a.remove();
}

It will download all images up to x number stipulated for for output.

inserir a descrição da imagem aqui

Obs: As said by Renan in the comments:

Remember that to work in a comfortable way the browser should be configured to download files automatically. Case otherwise it will wait for the user’s confirmation on where to save each file

  • Good idea. Remember that to work in a comfortable way the browser must be configured to download files automatically. Otherwise it will wait for the user’s confirmation about where to save each file.

  • True! I will add in response this observation

  • I tested but did not download the photo but an html page. LOL

  • http://uploader.centraldemangas.com.br/the_gamer/the_gamer035-01.jpg

  • Isn’t it because without an image link you don’t give direct access? or something like that, because when I access the link you gave me I’m directed to the website not to the image.

  • Oh I don’t know, that might be it?

  • I believe so

Show 2 more comments

2


A possible solution would be to create a tag and go picking up the images through them.

// Função para baixar a imagem...
function downloadImage(src) {
var img = document.createElement("img");
img.src = src;

    return img;
}

// Imagens baixadas:
var images = [];

// Loop para baixar as imagens...
for(var i = 1; i < 35; i++) {
    // Primeiro pegamos o valor de "i" e transformamos em uma string...
    var n = i.toString();

    // Colocar um zero a mais caso seja necessário...
    if(n.length < 2) {
        n = "0" + n;
    }

    // Agora é só criar uma tag <img> e ir colocando na Array...
    images.push(downloadImage("http:///the_gamer/the_gamer035-" + n + ".jpg"));
}

Then it would just be taking what was stored in the Array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.