1

My code like this :


const axios = require('axios').default
import FormData from 'form-data'
import fs from 'fs'

export default class MyController {
  public async handleMultipleFile({ request }: HttpContextContract) {
    if(request.files('files').length > 0){
      const formData = new FormData()
      
      for( let i = 0; i < request.files('files').length; i++ ) {
        let file = request.files('files')[i]
        formData.append('files[' + i + ']', file)
      }

      axios.post(`https://example-api.com/assets/files`, formData, {
          headers: {
              'Content-Type': 'multipart/form-data'
          },
        }
      ).then(res => console.log(res.data))
      .catch(err => console.error(err))
    }
  }
}

The request.files('files') like this :

{
    "fieldName": "files[]",
    "clientName": "test.xlsx",
    "size": 8212,
    "type": "application",
    "extname": "xlsx",
    "subtype": "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "state": "consumed",
    "isValid": true,
    "validated": true,
    "errors": [],
    "meta": {}
}

I want to convert this file to buffer/blob/binary. So I can using formData to pass the file to a api. Because when using formData on the server side, the file must be in binary/blob

How can I solve this problem?

Note :

The file is not saved in path. So when the file is uploaded, the file is sent directly to the external api

My code is backend (node js)

0 Answers0