jaydenseric / extract-files

A function to recursively extract files and their object paths within a value, replacing them with null in a deep clone without mutating the original value. FileList instances are treated as File instance arrays. Files are typically File and Blob instances.
https://npm.im/extract-files
MIT License
54 stars 23 forks source link

FileList handler does not work against a genuine FileList #29

Closed laurisvan closed 2 years ago

laurisvan commented 2 years ago

I am currently working with a GraphQL mutation passing multiple files as an input in form of [Upload!], but struggling to get it work. No matter what I do, the output from GraphQL does not seem to send the binaries, but an array of JSON.stringified Files.

Initially the problem was that I was passing an array of File instances, but after reading README of jaydenseric/apollo-upload-client, it seems that I should pass a FileList. Due to using a few 3rd party dependencies, I cannot get a native FileList directly from my code (only an array of Files), so I needed to adapt them to a FileList using a DataTransfer as follows:

    const xfer = new DataTransfer()
    this.files.forEach(f => xfer.items.add(f))
    return xfer.files

According to DataTransfer API (https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer), this should return a FileList, but that one still gets stringified. I checked the related test in this project, and it seems that it expects some object with files method which is not part of FileList.

What is the actual API that should be returned? It looks almost as if it were DataTransfer that should be passed - not FileList?

jaydenseric commented 2 years ago

You should be able to use a native FileList instance, or, File instances in arrays or as object properties, anywhere really.

laurisvan commented 2 years ago

Found the original problem - it was in my link handler (I checked from the values whether they were Files or Blobs, but did not handle arrays of Files or Blobs). After the change, it works just fine with an array of Files (File[]).

However, I cannot still understand the FileList related test in this project but cannot confirm it would not work. I'll close this issue, but I'd still recommend checking why and how the FileList implementation would work as it does not utilise a public api of FileList. I would assume the docs in MDN are correct, and they declare FileList class only has a method called item and a property called length (https://developer.mozilla.org/en-US/docs/Web/API/FileList). The .files() is a method of DataTransfer.