如何将 Express.Multer.File 转换为 Blob 数据?
How convert Express.Multer.File to Blob data?
我的控制器中有一个 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
})
}
像这样使用 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)
)
我的控制器中有一个 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
})
}
像这样使用 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)
)