Visitar URL original
How to send an array of pdf files using form-data · Issue #553 · form-data/form-data · GitHub
Skip to content

How to send an array of pdf files using form-data #553

Open

Description

@madhukiranvarma

I'm trying to send an array of pdf files to backend using form-data lib

I was trying to append pdf files like this:

const formData = new FormData();
for (const file of files) {
       formData.append('file[]', fs.createReadStream(file.path), {
               filename: `${file.type}_${file.month}.pdf`,
        });
}

and this is the axios request

Axios.request({
        method: 'POST',
        url,
        headers: {
            'Content-Type': data.formData.getHeaders()['content-type'],
        },
        data: data.formData,
    });

but I'm getting this error, everytime I make an axios request

node_modules/express/lib/response.js:1150
    : JSON.stringify(value);
           ^
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'curlObject' -> object with constructor 'CurlHelper'
    --- property 'request' closes the circle
    at JSON.stringify (<anonymous>)

can anyone please tell me what's the issue here?

Activity

  1. jimmywarting commented on Sep 6, 2023

    @jimmywarting

    I would say that you don't need this pkg anymore.

    const formData = new FormData()
    
    for (const file of files) {
      const blob = await fs.openAsBlob(file.path)
      formData.append('file[]', blob, `${file.type}_${file.month}.pdf`)
    }
    
    fetch(url, { method: 'POST', body: formData })

    fetch, blob and formdata is built in globally now.

  2. glhrmv commented on Sep 6, 2023

    @glhrmv

    @jimmywarting I am getting Cannot find name 'FormData'. compilation error from TypeScript using Node 18.16.1, is there something I need to configure to let TypeScript aware of this global?

  3. jimmywarting commented on Sep 6, 2023

    @jimmywarting

    if you are going to use fs.openAsBlob(path[, options]) then you will need v19.8+
    maybe try to install latest @types/node

  4. glhrmv commented on Sep 6, 2023

    @glhrmv

    @jimmywarting Should have clarified that I am not using openAsBlob, the issue is strictly related to the first line const formData = new FormData(). And I have the latest @types/node. :(

  5. jimmywarting commented on Sep 6, 2023

    @jimmywarting

    I am not using openAsBlob

    Built in FormData is not going to accept a readableStream... if that is what you are using. saw you where using fs.createReadStream(file.path)

    And I have the latest @types/node. :(

    sry, can't help you there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions