What to pass when the type is `Record<string, File>`

Viewed 36

I am trying to upload some files and the data accepted type is Record<string, File>

Here is the function

export class FileService {
  async uploadFile(
    fileId: string,
    {
      documents,
      checkbox,
    }: { documents?: Record<string, File>; checkbox?: boolean }
  ): Promise<FileEntry> {
    const form = new FormData();

    Object.entries(documents || {}).forEach(([k, file]) =>
      form.append(k, file, file.name)
    );
    return ...
  }
}

So you can see there are two variables documents and checkbox. Checkbox is a boolean a value so I understand. What I am not clear is Record<string, File>

I tried passing the data like this for documents

FileService.uploadFile('testing', {
  documents: {
     "driverLicense": File{ "name": "testing.jpg", lastModified: 1234331, "size": 123},
     "signature": File {name: "testing.jpg", lastModified: 1234331, "size"123}
  },
  checkbox: true,
});

In the browser something like this

data that is being posted

But I always get 400 error. But when I try to pass documents as empty object its works.

   FileService.uploadFile('testing', {
      documents: {},
      checkbox: true,
    });

Does anyone know why I am failing?

Here is the api tested in post man which is working fine

Post man test

useEffect(() => {
        if (inputRef.current) {
          const listener = () => {
            const files: FileList | null | undefined = inputRef.current?.files;
           //File is being passed here
            if (files) {
              onFileChange(files[0]);
            }
          };
        }
        return () => {
          /* */
        };
      }, [onChange, onFileChange]);
1 Answers
Related