question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose some method add files to a FileList

See original GitHub issue

FileList’s are not writeable in the specs, but to make input.files testable, we’ll need to implement a util method to modify it.

/cc @cpojer

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:11
  • Comments:33 (11 by maintainers)

github_iconTop GitHub Comments

14reactions
niksajanjiccommented, Dec 5, 2016

I managed to create FileList without having to alter any of the jsdom library code:

const createFile = (size = 44320, name = 'ecp-logo.png', type = 'image/png') =>
  new File([new ArrayBuffer(size)], name , {
    type: type,
  });

const createFileList = (file) => {
  const fileList = new FileList();
  fileList[0] = file;
  return fileList;
}

const fileList = createFileList(createFile());

In this case I’m creating FileList object calling constructor on FileList provided by jsdom. After that I’m just adding file to that list using Array notation. In my case I only need 1 File in array but this can also be changed to for loop to add multiple files to FileList. I’m creating file with custom name/type/size through File constructor also provided by jsdom which functionality is following specification.

This might be helpful for someone who is looking how to mock FileList in jsdom environment, but one issue still stands. Creating FileList with array of Files using this method wouldn’t allow getting items from array using FileList.item(index) method. But, even that can be fixed by overriding it’s method something like this:

const createFileList = (file) => {
  const fileList = new FileList();
  fileList[0] = file;
  fileList.item = index => fileList[index]; // override method functionality
  return fileList;
}

I still feel it might be better if jsdom could offer these functionalities for testing purposes out of the box.

7reactions
tmorehousecommented, Jul 13, 2019

All modern browsers (i.e. not IE <= 11) now support setting input.files to a FileList https://stackoverflow.com/a/47522812/2744776

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to work with FileList (from <input type="file">) in Javascript?
You can use FormData to append Files to a single object. var data = new FormData(); document.querySelector("input[type=file] ...
Read more >
The Trouble With Editing And Uploading Files In The Browser
Unfortunately there's no FileList constructor. There is also no “add a file” method exposed on the FileList instance.
Read more >
How to Handle File Inputs With JavaScript | by John Au-Yeung
We can trigger a file selection dialog to open with the click method of the file input object. Then we can hide the...
Read more >
Dockerfile reference - Docker Documentation
Environment variables are supported by the following list of instructions in the Dockerfile : ADD; COPY; ENV; EXPOSE; FROM; LABEL; STOPSIGNAL ...
Read more >
FileList - Web APIs | MDN
An object of this type is returned by the files property of the HTML <input> element; this lets you access the list of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found