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

前端 未结 4 631
渐次进展
渐次进展 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";  
                }
            }
        })
    
    0 讨论(0)
  • 2020-12-02 18:25

    JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to make it possible to interact with octet streams in the context of things like TCP streams and file system operations.

    Pure JavaScript, while great with Unicode encoded strings, does not handle straight binary data very well.

    When writing large amounts of data to a socket it's much more efficient to have that data in binary format vs having to convert from Unicode.

    var fs = require('fs');
    // string generated by canvas.toDataURL()
    var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
        + "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
        + "3gAAAABJRU5ErkJggg==";
    // strip off the data: url prefix to get just the base64-encoded bytes
    var data = img.replace(/^data:image\/\w+;base64,/, "");
    var buf = Buffer.from(data, 'base64');
    fs.writeFile('image.png', buf);
    

    Reference

    0 讨论(0)
  • 2020-12-02 18:27

    I have had the question in question. I solved the problem when I made the default value null of "encoding" in the "request" library

    var request = require("request").defaults({ encoding: null });
    var fs = require("fs");
    
    fs.writeFile("./image.png", body, function(err) {
        if (err) throw err;
    });
    
    0 讨论(0)
  • 2020-12-02 18:33

    Use Buffer.from, as Buffer is deprecated, will get the following warning

    (node:15707) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

    var fs = require('fs');
    // string generated by canvas.toDataURL()
    var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
        + "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
        + "3gAAAABJRU5ErkJggg==";
    // strip off the data: url prefix to get just the base64-encoded bytes
    var data = img.replace(/^data:image\/\w+;base64,/, "");
    var buf = Buffer.from(data, 'base64');
    fs.writeFile('image.png', buf);
    
    0 讨论(0)
提交回复
热议问题