0

When I add files programmatically to my <input multiple> element, the length attribute is not modified. If I dig into ti it shows that the files are contained in the input element, but the length stays at zero. I tried manually setting it, but I think the length value is read-only. How can I get the length to update?

//add files to cleared input multiple element
for(i=0; i<newFileListLength; i++){
    document.getElementById('documentInput').files[i] = newListOfFiles[i];
}

// change file length value (DOESN'T WORK)
document.getElementById('documentInput').files.length = newFileListLength;
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
ProgrimerGuy
  • 63
  • 1
  • 6
  • "add files programmaticaly". Could you please share code that adds files programmaticaly without user selecting them manually? – Yuriy Galanter Feb 01 '16 at 21:43
  • Well, my code is allowing users to delete files individually when they upload them in bulk. So if a user uploads 4 files, they can just delete 1 of the files without having to re-select the 3 files they want. So, with javascript, I am taking the files already uploaded, storing them in a variable, clearing the then adding the remaining files with a loop. (I will add the code above) – ProgrimerGuy Feb 02 '16 at 15:27

1 Answers1

0

You can't. .files is a read only property, so attempts to overwrite it will not result in error, but will silently fail.

http://www.w3schools.com/jsref/prop_fileupload_files.asp

See also How do I remove a file from the FileList

Community
  • 1
  • 1
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • Bummer :(. Out of curiosity, how/why am I able to add files then? – ProgrimerGuy Feb 02 '16 at 16:28
  • You can use the `FormData` object to upload files over ajax, and this actually does give you more control over file submissions, but the downside is that it is ajax, not a standard form submit. https://developer.mozilla.org/en-US/docs/Web/API/FormData – chiliNUT Feb 02 '16 at 16:51