Remove selected file(s) before upload with

2019-01-08 00:55发布

问题:

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)?

回答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



回答2:

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>


回答3:

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/