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.

FileList is supported but not an array/object with multiple files.

See original GitHub issue

Hi. I’m facing with an issue where I’m able to get multiple files when making a simple <input type="file" multiple> but I’m facing issues when trying to integrate Dropzone UI which is giving me back an Array of files.

I’m using type-graphql with apollo-server-exprees. Here is my resolver:

@Mutation(() => Boolean)
@UseMiddleware(isAdmin)
async fileUpload(
  @Arg('files', () => [GraphQLUpload])
  filesArray: Array<FileUpload>,
) {
  filesArray.map(async (item) => {
    const { createReadStream, filename } = await item;

    const fileRenamer = (filename: string): string => {
      const queHoraEs = Date.now();
      const regex = /[\s_-]/gi;
      const fileTemp = filename.replace(regex, '.');
      let arrTemp = [fileTemp.split('.')];
      return `${arrTemp[0]
        .slice(0, arrTemp[0].length - 1)
        .join('_')}${queHoraEs}.${arrTemp[0].pop()}`;
    };

    const stream = createReadStream();
    const assetUniqName = fileRenamer(filename);
    const pathName = path.join(__dirname, `../uploads/${assetUniqName}`);
    await stream.pipe(fs.createWriteStream(pathName));
    const urlForArray = `${process.env.SERVER_URL}/${assetUniqName}`;
    console.log(urlForArray);
  });
  return true;
}

And here is a picture of what I send (In the top the ‘failing’ in the bottom the ‘success’. In the top I manually made an object with the array of files I get from dropzone in the bottom is what I get from the regular input file)

Captura de Pantalla 2022-01-16 a la(s) 15 52 38

I find out that FileList is a read-only and we can’t create one from 0. So I’m stuck here. Also Attaching a picture of the answer I get from the BE.

image

And this of course is only happening when trying to push from the Object I created or from the Array I got from Dropzone (tried both ways).

Any help is very much appreciated.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mateusmxcommented, Jan 18, 2022

Hi @jaydenseric. I was able to make it work. Apparently when I was sending the array from the Dropzone integration, it was being ‘rejected’ with the errors shared before. So what was needed to do is to iterate that array from Dropzone and create a new one with only the file. I’ll try to get sometime to write an article and share it so more persons can solve this.

Thank you so much for your help.

0reactions
jaydensericcommented, Jan 17, 2022

@mateusmx consider carefully this part of your query: $files: [Upload!]!. What you are doing in variables JSON is not that. You are doing an array that incorrectly contains objects that have the Upload scalar nested a layer deeper.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why isn't the FileList object an array? - Stack Overflow
Why is FileList an object rather than an array? The only property it has is .length and the only method it has is...
Read more >
FileList - Web APIs | MDN
Returns a File object representing the file at the specified index in the file list. Example. Logging filenames. In this example, we log...
Read more >
Class: Rake::FileList — Documentation for rake (11.1.2)
A FileList is essentially an array with a few helper methods defined to make file manipulation a bit easier. FileLists are lazy.
Read more >
getting "Cell contents reference from a non-cell array object" error ...
After I run this code i am "Cell contents reference from a non-cell array object" error Pl help. 1 Comment. ShowHide None. Image...
Read more >
Expose some method add files to a FileList #1272 - GitHub
I can't seem to find it anymore. 2.) window.URL.createObjectURL isn't supported by jsdom. Not sure if it should be reported.
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