I want to generate thumbnails of images that the user selects. My problem is that one of the images always generates 3 thumbnails
Demo here: link
I want to generate thumbnails of images that the user selects. My problem is that one of the images always generates 3 thumbnails
Demo here: link
You should not use for .. in
to iterate over arrays (or array-likes ). It is used to iterate over the properties of an object. Since your FileList
has three properties ( "0"
, "length"
and "item"
) the code will execute three times.
Replace with% common%:
for (var key = 0 ; key < files.length ; key++)
The problem is the use of for (... in ...)
, which also reads the properties of the array of files, such as length
. That is, you are adding an image when the length property is iterated!
EDIT An aid to generate the thumb:
Just to supplement the response, you can resize the img
element to maintain the proportions of the original image by setting the following style for it:
max-width: 20px; /* qualquer largura máxima desejada */
width: auto;
height: auto;