I have a set of inputs and images:
I am looping through the divs
to get the names and the photos:
$('.subCats').each(function () {
let id = genRandomStr(),
name = $(this).find('input.subCatName').val();
if (name.length > 0) {
dataObj['subCats'].push({
'id' : id,
'name' : $(this).find('input.subCatName').val()
});
$(this).find('.media-list img').each(function () {
if ($(this).data('stored') === false)
urlToFile($(this).attr('src'), $(this).attr('title'), 'image')
.then(function (file) {
data.append('subCatsPhotos[' + id + ']', file);
});
});
}
});
I have an AJAX request just after but the problem is the request is executing before the image has been appended to the FormData
.
This is the code that is converting the image to a file object:
function urlToFile(url, filename, mimeType) {
return (fetch(url)
.then(function(res){return res.arrayBuffer();})
.then(function(buf){return new File([buf], filename,{type:mimeType});})
);
}
I have looked at these solutions but I am struggling to adapt them to my code.