Remove selected file(s) before upload with Javascript
Solution 1
FileList has no API to remove entries:
https://developer.mozilla.org/en/DOM/FileList
However you can reconstruct File uploader using XHR2 and AJAX and filter in content there.
This implies doing XHR2 and AJAX upload and is not suitable for traditional <form>
uploads.
https://developer.mozilla.org/en/Using_files_from_web_applications
Solution 2
Libraries are your best bet before we get some standard API...
FineUploader. Demo. Has file delete, image preview, file rename.
bluimp's jQuery File Upload Plugin. Demo. Has file delete, image preview, individual file upload, upload undo.
They are also available at https://cdnjs.com/
Solution 3
If you are using a standard form with a set of standard file inputs then you can do it like this http://jsfiddle.net/thXre/
$(document).ready(function(){
$('.remove').click(function(){
$(this).closest('div').slideUp('slow', function(){$(this).remove();});
});
});
and html code:
<div>
<input type='file' name='files[]'> <img src='x.gif' class='remove'>
</div>
<div>
<input type='file' name='files[]'> <img src='x.gif' class='remove'>
</div>
<div>
<input type='file' name='files[]'> <img src='x.gif' class='remove'>
</div>
bob_cobb
Updated on February 28, 2020Comments
-
bob_cobb about 4 years
Say I have a form that allows users to upload multiple images, which are appended with an option to remove them if they don't want to upload that particular photo.
Is it possible to remove the value from the files object of the one that they removed (e.g. didn't want to upload)?
-
bob_cobb over 12 yearsThanks, I know how to remove the actual elements, but it's removing them from being included in the upload that I was interested in which FileList takes care of.
-
bob_cobb over 9 years2 years later... FileList still doesn't in any way shape or form support removing entries within its API.
-
Ciro Santilli OurBigBook.com over 9 years@bob_cobb it's a sad future =)
-
jbalintac about 7 yearsFuture's here, still looking for a proper way to remove a file ─ doesn't seems to have one yet.