wxh
wxh

Reputation: 808

Argument of type 'ReadStream' is not assignable to parameter of type 'string | Blob' when formData.append(...) for file upload

I am writing an extension that needs to upload selected file via multipart formdata.

I found some snippets, like Cannot POST a multipart/form-data using Fetch TYPESCRIPT, and wrote something similar:

        window.showOpenDialog({
            canSelectFiles: true,
            canSelectFolders: false,
            canSelectMany: false,
            filters: {
                'image': ['jpg', 'png'],
            },
            title: 'Add Image',
        }).then(async uriList => {
            if (undefined === uriList || uriList.length === 0) {
                return;
            }

            let imageFile = createReadStream(uriList[0].fsPath);

            let formData = new FormData();
            formData.append('file', imageFile); // <- ts(2345) error
        });

However it complains:

Argument of type 'ReadStream' is not assignable to parameter of type 'string | Blob'.
  Type 'ReadStream' is missing the following properties from type 'Blob': size, type, arrayBuffer, slice, and 2 more.ts(2345)

What am I missing?

Upvotes: 1

Views: 9074

Answers (2)

mat.twg
mat.twg

Reputation: 745

FormData can only be imported by using import FormData = require("form-data") or by turning on the esModuleInterop flag and using a default import.

Upvotes: 0

AmiNadimi
AmiNadimi

Reputation: 5725

As @Nalin Ranjan mentioned in comment, using

import FormData from "form-data";

Fixed the problem.

Upvotes: 9

Related Questions