Skip to content

Instantly share code, notes, and snippets.

@Minobi
Last active October 7, 2021 12:15
Show Gist options
  • Save Minobi/d1636982a6a545ffe09070f15becca77 to your computer and use it in GitHub Desktop.
Save Minobi/d1636982a6a545ffe09070f15becca77 to your computer and use it in GitHub Desktop.
Add and remove file from non-modifying FileList in input type "file"
function addFile(fileInput, event) {
const dt = new DataTransfer();
Array.from(fileInput.files).forEach(file => {
dt.items.add(file);
});
Array.from(event.dataTransfer.files).forEach(file => {
dt.items.add(file);
});
fileInput.files = dt.files;
}
function removeFile(fileInput, fileName) {
const dt = new DataTransfer();
Array.from(fileInput.files).forEach(file => {
if (file.name != fileName) {
dt.items.add(file);
}
});
fileInput.files = dt.files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment