How to remove image from file list and upload it to server using api request?
I am using Html multi-select to select images and apply javascript to display images. As you can see in this figure, a cross is on top of each image, which removes the images from the user interface. So, if from the three displayed images I remove one of them and upload it to the server, it will download all three imgs instead of two images.
my script:
$(document).on('click', '.browse', function () {
var file = $(this).parent().parent().parent().find('.file');
file.trigger('click');
});
$(document).on('change', '.file', function () {
$(this).parent().find('.form-control').val($(this).val().replace(/C:\\fakepath\\/i, ''));
});
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
if (i > 3) {
break;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumbs_image" src="', e.target.result,
'" title="', escape(theFile.name), '"/> <a href="#" class="remove_pic">X</a>'
].join('');
document.getElementById('list').insertBefore(span, null);
span.children[1].addEventListener("click", function (event) {
span.parentNode.removeChild(span);
});
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
+3
source to share
1 answer
Unfortunately it cannot be done like this: you cannot remove the file from that html input element because the selected files / images are stored in a read-only FileList object: https://developer.mozilla.org/en-US/docs / Web / API / FileList
Alternatively, you can keep your own array of files, but then you will need to use your own implementation to load the files.
0
source to share