Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Upload File to Strapi Using FormData

hi im having trouble on uploading a file to strapi from backend nodejs im doing

bodyFormData.append('files.image', fs.createReadStream(req.file.path), req.file.filename);
bodyFormData.append('data', JSON.stringify(data));

and using axios to create a post request

axios({
        url:`${strapi}/campaigns`,
        method:'post',
        data:bodyFormData,
        withCredentials:true,
      })
      .then(response=>{
        console.log(response.data)
        // return res.status(200).send(response.data)
      })
      .catch(err=>{
        res.status(400).send(err)
      })

here's my console.log(bodyFormData)

FormData {
[0]   _overheadLength: 288,
[0]   _valueLength: 272,
[0]   _valuesToMeasure: [],
[0]   writable: false,
[0]   readable: true,
[0]   dataSize: 0,
[0]   maxDataSize: 2097152,
[0]   pauseStreams: true,
[0]   _released: false,
[0]   _streams: [
[0]     '----------------------------167862620653181372969530\r\n' +
[0]       'Content-Disposition: form-data; name="files.image"; filename="1582728780533-fancy-bottom-border.png"\r\n' +
[0]       'Content-Type: image/png\r\n' +
[0]       '\r\n',
[0]     'public\\1582728780533-fancy-bottom-border.png',
[0]     [Function: bound ],
[0]     '----------------------------167862620653181372969530\r\n' +
[0]       'Content-Disposition: form-data; name="data"\r\n' +
[0]       '\r\n',
[0]     '{"title":"123`${strapi}/campaigns`","description":"`${strapi}/campaigns`","author":{"id":62},"reference":30939226,"goal":"123","verified":false,"deleted":false,"requested":false,"raised":0,"username":"cjoyales","currency":"USD"}',
[0]     [Function: bound ]
[0]   ],
[0]   _currentStream: null,
[0]   _insideLoop: false,
[0]   _pendingNext: false,
[0]   _boundary: '--------------------------167862620653181372969530'
[0] }

but here's the console.log(response.data) from strapi

{
[0]   id: 176,
[0]   title: '',
[0]   description: '',
[0]   goal: 0,
[0]   raised: null,
[0]   username: null,
[0]   created_at: '2020-02-26T14:53:00.000Z',
[0]   updated_at: '2020-02-26T14:53:00.000Z',
[0]   verified: null,
[0]   deleted: null,
[0]   author: null,
[0]   reference: null,
[0]   currency: null,
[0]   requested: null,
[0]   done: null,
[0]   image: [],
[0]   supporters: []
[0] }

why is it empty or null even there are values in the form data? can someone help? Thanks

like image 629
Cj Oyales Avatar asked Dec 06 '25 05:12

Cj Oyales


1 Answers

Try to replace

bodyFormData.append('files.image', fs.createReadStream(req.file.path), req.file.filename);

with

bodyFormData.append('files', fs.createReadStream(req.file.path), req.file.filename);

I struggled for hours with something similar until I read Pavlo Razumovskyi's comment here:

It's new api now, check new docs: strapi.io/documentation/3.0.0-beta.x/plugins/… formData.append('files.MODEL_FILE_FIELD_NAME', blob, filename)

like image 160
speexy Avatar answered Dec 07 '25 20:12

speexy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!