1
<input 
    type="file"
    id="fileElem" 
    multiple 
    accept="image/*"
    style="display:none"
    onchange="handleFiles(this.files)">

<a href="#" id="fileSelect">Select some files</a> 

<div id="fileList">
    <p>No files selected!</p>
</div>

When I selected 5 files: How to dynamically remove some of them?

I know how clear all files How can I clear an HTML file input with JavaScript?

1 Answer 1

1

$("#fileElem")[0].files is a read-only array.

You can get around this by pushing those files into a separate Array though. You can then do whatever you want with that curated list of files. If uploading them to a server is the goal, you can use the FileReader API.

Below is a round about way of completely avoiding needing to modify the FileList. Steps:

  1. Add normal file input change event listener
  2. Loop through each file from change event, filter for desired validation
  3. Push valid files into separate array
  4. Use FileReader API to read files locally
  5. Submit valid, processed files to server

Not the answer you're looking for? Browse other questions tagged or ask your own question.