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
2 Answers
Reset to default 3Sometime 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
版权声明:本文标题:javascript - How can I solve source.on is not a function when using form data? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741879340a2402653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论