Writing binary data using node.js fs.writeFile to create an image file

前端 未结 4 636
渐次进展
渐次进展 2020-12-02 17:46

I\'m trying to write a canvas data with node.js fs.writeFile as a binary. JPEG file, but after the file is written I can see that the file is stored as plain text, not binar

4条回答
  •  一生所求
    2020-12-02 18:20

    Instead of writing the file directly to your client, first, ask the server to send images in binary format.

       let request= {
            headers: {
                'Content-Type': 'image/jpeg',
                'Authorization': "your token"
            },
            encoding:'binary'
        };
         request.get(url,request,(error, response, body)=>{
            if(error){
                console.log('error in get photo',error)
                return "default image to server";  
            }else{
                if(response.statusCode == 200){ 
    
          Fs.writeFile('path',body,'binary',function(err){
                        if(err){
                            return "your message";   
                        }else{
                            return "success";
                        }
                    })
                }else{
                    console.log('error in get photo 3')
                    return "your message";  
                }
            }
        })
    

提交回复
热议问题