Skip to content

Instantly share code, notes, and snippets.

@brianmriley
Created November 20, 2018 20:17
Show Gist options
  • Save brianmriley/726090f977eb7aa2dcca28209f266612 to your computer and use it in GitHub Desktop.
Save brianmriley/726090f977eb7aa2dcca28209f266612 to your computer and use it in GitHub Desktop.
Demonstrates how one can programmatically set a HTML file input's value.
// Taken from SO: https://stackoverflow.com/questions/47119426/how-to-set-file-objects-and-length-property-at-filelist-object-where-the-files-a
const dT = new ClipboardEvent('').clipboardData || // Firefox < 62 workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655
new DataTransfer(); // specs compliant (as of March 2018 only Chrome)
dT.items.add(new File(['foo'], 'programmatically_created.txt'));
inp.files = dT.files;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment