我的控制器中有一个NestJs项目,我接受Express.Multer.File,但我需要将其发送到另一台服务器。如何将其转换为Blob以传递给FormData?
#action in my controller
@Post('/upload')
@UseInterceptors(FileInterceptor('avatar'))
async uploadUserProfileAvatar(@UploadedFile() file: Express.Multer.File){
console.log(file)
let blob = new Blob(file.mimetype)
let formData = new FormData()
//can't set because file is not Blob
formData.append('file',file)
let request$ = this.httpService.post('http://test_server/file',formData,{headers:{
'Content-Type':'multipart/form-data'
}}).pipe(
map(response => response.data)
)
request$.subscribe( response => {
console.log(response)
return response
})
}
我将不胜感激每一个答案!
编辑:
再次感谢你的帮助!结果,我设法以这种方式成功发送了代码:
@Post('/upload')
@UseInterceptors(FileInterceptor('avatar'))
async uploadUserProfileAvatar(@UploadedFile() file: Express.Multer.File){
const FormData = require('form-data');
const formData = new FormData();
formData.append('file', Buffer.from(file.buffer) , file.originalname);
let request$ = this.httpService.post('http://nginx_files/file',formData,{headers:formData.getHeaders()}).pipe(
map(response => response.data)
)
request$.subscribe( response => {
console.log(response)
return response
})
}
uj5u.com热心网友回复:
使用这样的formdata
包:
const FormData = require('form-data');
const formData = new FormData();
formData.append('file', fileContent, 'file_name.ext');
let request$ = this.httpService.post('http://test_server/file',
formData,
{ headers: formData.getHeaders() }
).pipe(
map(response => response.data)
)
0 评论