【问题标题】:ReadFile in Base64 NodejsBase64 Nodejs 中的读取文件
【发布时间】:2015-05-04 06:47:46
【问题描述】:

我正在尝试从客户端读取以 base64 编码的图像。 nodejs如何阅读?

我的代码:

// add to buffer base64 image
var encondedImage = new Buffer(image.name, 'base64');

fs.readFile(encondedImage, "base64", function(err, buffer){
    if ( err ) {
        console.log('In read file')
        console.log(err)
    } else {
        // check err
        lwip.open(buffer, 'jpg', function(err, image){
            console.log('in open')
            if ( err ) console.log(err)

            if ( image ) console.log(image)
            // check 'err'. use 'image'.
            // image.resize(...), etc.
        });
    }
})

但是,我收到了这个错误:

In read file
[Error: Path must be a string without null bytes.]

【问题讨论】:

  • fs.readFile 将字符串作为其第一个参数。您正在尝试将其传递给 Buffer

标签: javascript node.js image base64 readfile


【解决方案1】:

最新最好的方法:

Node 支持文件和缓冲区操作,base64 encoding:

const fs = require('fs');
const contents = fs.readFileSync('/path/to/file.jpg', {encoding: 'base64'});

或者使用新的promises API

const fs = require('fs').promises;
const contents = await fs.readFile('/path/to/file.jpg', {encoding: 'base64'});

【讨论】:

    【解决方案2】:

    我认为以下示例演示了您的需求: 我删除了链接,因为它已损坏。

    文章的精髓就是这段代码部分:

    var fs = require('fs');
    
    // function to encode file data to base64 encoded string
    function base64_encode(file) {
        // read binary data
        var bitmap = fs.readFileSync(file);
        // convert binary data to base64 encoded string
        return new Buffer(bitmap).toString('base64');
    }
    
    // function to create file from base64 encoded string
    function base64_decode(base64str, file) {
        // create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded
        var bitmap = new Buffer(base64str, 'base64');
        // write buffer to file
        fs.writeFileSync(file, bitmap);
        console.log('******** File created from base64 encoded string ********');
    }
    
    // convert image to base64 encoded string
    var base64str = base64_encode('kitten.jpg');
    console.log(base64str);
    // convert base64 string back to image 
    base64_decode(base64str, 'copy.jpg');
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 感谢您的解释。现在我明白了为什么我会遭到反对。
    • 如果目标是创建一个 base64 编码的data URI 解决方案有点短。实际数据正确,但字符串缺少data:[<mediatype>][;base64],前缀。
    • new Buffer 是多余的。 fs.readFileSync 如果未指定编码,则返回缓冲区。你可以做fs.readFileSync([..]).toString('BASE64')
    • @Gajus 你甚至可以走得更远。 readFileSync 现在可以为您进行编码。就做fs.readFileSync('...', { encoding: 'BASE64' })
    【解决方案3】:
    var fs = require('fs');
    
    function base64Encode(file) {
        var body = fs.readFileSync(file);
        return body.toString('base64');
    }
    
    
    var base64String = base64Encode('test.jpg');
    console.log(base64String);
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      相关资源
      最近更新 更多