Home > Blockchain >  How convert Express.Multer.File to Blob data?
How convert Express.Multer.File to Blob data?

Time:12-27

I have a NestJs project in my controller I accept an Express.Multer.File but I need to send it to another server. How can I convert it to Blob for passing to 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
        })
    }

I will be grateful for every answer!

CodePudding user response:

Use formdata package like this:

  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)
        )
  • Related