Created
November 8, 2020 03:01
-
-
Save g-rodigy/205e7b1e2c2c4de07d56c35561e4b5d7 to your computer and use it in GitHub Desktop.
Set the FileList of <input type="file"> to arbitrary File objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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