拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何将Express.Multer.File转换为Blob资料?

如何将Express.Multer.File转换为Blob资料?

白鹭 - 2022-02-23 2191 0 0

我的控制器中有一个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 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *