Skip to content

Instantly share code, notes, and snippets.

@g-rodigy
Created November 8, 2020 03:01
Show Gist options
  • Save g-rodigy/205e7b1e2c2c4de07d56c35561e4b5d7 to your computer and use it in GitHub Desktop.
Save g-rodigy/205e7b1e2c2c4de07d56c35561e4b5d7 to your computer and use it in GitHub Desktop.
Set the FileList of <input type="file"> to arbitrary File objects
// @source https://embed.plnkr.co/HpBkr4/
// some data or data from file input
const data = [
new File(["a"], "a.txt"), new File(["b"], "b.txt")
];
// https://github.com/w3c/clipboard-apis/issues/33
class _DataTransfer {
constructor() {
return new ClipboardEvent("").clipboardData || new DataTransfer();
}
}
const input = document.createElement("input");
input.type = "file";
input.name = "files[]";
input.multiple = true;
input.id = "files";
const dt = new _DataTransfer();
for (let file of data) {
dt.items.add(file)
}
if (dt.files.length) {
input.files = dt.files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment