Upload Images

Asked

Viewed 33 times

0

I’m having a problem uploading images from a file(folder) to the web page. When you start the page, only two images are loaded, the others fail! I’m using Javascript and Jquery, css/html.

var $galery = $('.galery');

function fetchArticles(contents){

    for(var i= 0 ; i <contents.length ; i ++){

        console.log(contents[i].images[0].id);

        $galery.append('<h2>'+contents[i].name+'</h2>');
        $galery.append('<p>'+contents[i].texto+'</p><br>');
        $galery.append('<p>'+contents[i].images[0].id+'</p><br>');
        var folder = "/static/img/ContentPhotos/";
        $galery.append('<img src='+folder+contents[i].images[0].name+' width="300" height="250"></img>');
    }
}
  • seeing only the code that you made available, apparently it is correct, you are passing the name of the files correctly? try using encodeURI(Folder+Contents[i].images[0].name) to see if it solves

  • My code: <img src="encodeURI('+folder+')" width="300" height="250" /> Result: <img src="encodeURI(/Static/img/Contentphotos/e2e000f4-7d75-4ef3-b825-a0d00c7cf535.jpg)" width="300" height="250" />

  • With my code the page loads the first two images, the remaining ones that start to come out broken!

  • And yes Vinicius, the names are being passed correctly. I’m afraid how this is happening in a loop of images that’s inside another loop of content, and it’s disfigured!

1 answer

0

The estate src is without quotes. The tag <img> also does not need closure </img>

Follow the corrected code:

$galery.append("<img src='" + folder + contents[i].images[0].name + "' width='300' height='250'>");

  • Dear Michel Lima, I have tested it in every way. $Galery.append('<img src="'+Folder+'" width="300" height="250" />')

  • Then put all html inside a variable and then use append. var html = "";&#xA; for(var i= 0 ; i <contents.length ; i ++){&#xA; html += '<h2>'+contents[i].name+'</h2>';&#xA; html += '<p>'+contents[i].texto+'</p><br>';&#xA; html += '<p>'+contents[i].images[0].id+'</p><br>';&#xA;&#xA; var folder = "/static/img/ContentPhotos/";&#xA;&#xA; html += "<img src='" + folder + contents[i].images[0].name + "' width='300' height='250'>";&#xA; console.log(html);&#xA;&#xA; }&#xA;}&#xA;$galery.append(html); .

Browser other questions tagged

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