My app can create previews of selected images, put them into a table cell and let me fill necessary information of each image (name, tags, source) in other cells of the same row before uploading. But there is a problem: I use FileReader
and his function readAsDataURL
which is asynchronous. This means that I recieve the previews in wrong order and after uploading they don't match with other information about photos. Here you can see the result on the
screenshot.
I found some ideas about this issue, but I don't know how to implement them in my case, when I put every preview in separeted table cell and create those cells dynamically.
EDIT: the order of file uploading to the server is correct. Incorrect is only the order of previews I recieve before such uploading. I mean, if I have files 1, 2, 3 on my disc, I can recieve the previews in order 3, 1, 2. But to the server the files will be uploaded as 1, 2, 3.
Here is my function:
var newElem = document.createElement('table');
newElem.id = 'tl';
newElem.align = 'center';
newElem.border = 0;
for (var i = 0; i < countFiles; i++) {
var reader = new FileReader();
reader.onload = function (e) {
//create cells for each field
var newRow = newElem.insertRow(0);
var newCell1 = newRow.insertCell(0);
newCell1.innerHTML = "<input type='text' class='form-control' " +
"placeholder='Source' name='source' style='margin: 15px'>";
var newCell2 = newRow.insertCell(0);
newCell2.innerHTML = "<input type='text' class='form-control' " +
"placeholder='Tags' name='tags' style='margin: 10px'>";
var newCell3 = newRow.insertCell(0);
newCell3.innerHTML = "<input type='text' class='form-control' " +
"placeholder='Name' name='name' style='margin-left: 5px'>";
var newCell4 = newRow.insertCell(0);
//append the preview
$("<img />", {
"src": e.target.result,
"class": "thumb-image"
}).appendTo(newCell4);
};
document.getElementById("image-holder").appendChild(newElem);
reader.readAsDataURL($(this)[0].files[i]);
image_holder.show();
}