admin管理员组

文章数量:1312763

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(``, formData, {
          headers: {
              'Content-Type': 'multipart/form-data'
          },
        }
      ).then(res => console.log(res.data))
      .catch(err => console.error(err))
    }
  }
}

When I call the code from postman, I find this error "message": "source.on is not a function"

I add return to see the formData in the postman

I had install library form data and axios in my code and I had import it

I want to post multiple file to a external api

How can I solve this problem? Please help. Thanks

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./assets/files`, formData, {
          headers: {
              'Content-Type': 'multipart/form-data'
          },
        }
      ).then(res => console.log(res.data))
      .catch(err => console.error(err))
    }
  }
}

When I call the code from postman, I find this error "message": "source.on is not a function"

I add return to see the formData in the postman

I had install library form data and axios in my code and I had import it

I want to post multiple file to a external api

How can I solve this problem? Please help. Thanks

Share Improve this question edited Aug 4, 2022 at 16:34 positive developer asked Aug 3, 2022 at 20:21 positive developerpositive developer 1371 gold badge1 silver badge13 bronze badges 13
  • Your code snippet does not contain source.on. Where is that in your code? Please add it to your question. – jasie Commented Aug 4, 2022 at 6:03
  • 1 @jasie I's not in the code. The error happens when call formData.append. This seems to happen only in adonis – positive developer Commented Aug 4, 2022 at 6:36
  • ah sorry. did you check this? stackoverflow./questions/57788965/… – jasie Commented Aug 4, 2022 at 6:40
  • @jasie I have read that before. But I'm still can't solve my case – positive developer Commented Aug 4, 2022 at 6:51
  • pity.. what about this one? stackoverflow./questions/69763553/… – jasie Commented Aug 4, 2022 at 7:13
 |  Show 8 more ments

2 Answers 2

Reset to default 3

Sometime the data of the file is an object with the property value where is the content of the file. You have to append the file like this:

formData.append('files[' + i + ']', file.value,'NameOfFile.pdf')

In my case, the issue was because I was passing a Blob as a value to append:

const FormData = require('form-data');

. . .

const formData = new FormData();
formData.append('file', someBlobData, { filename: 'name' });

As soon as I passed a Buffer instead:

const formData = new FormData();
formData.append('file', someBuffer, { filename: 'name' });

It was fixed.

For me, I already had a Buffer that I was converting into a Blob, so the fix was just to not do that conversion. If you only have a Blob, see here: Convert Blob data to Raw buffer in javascript or node

本文标签: javascriptHow can I solve sourceon is not a function when using form dataStack Overflow