Base64 Nodejs 中的读取文件 [英] ReadFile in Base64 Nodejs

查看:61
本文介绍了Base64 Nodejs 中的读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to read an image from client side encoded in base64. How to read with 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.
        });
    }
})

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

But, I got this error:

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

推荐答案

最新最好的方法:

Node 支持使用 base64 编码的文件和缓冲区操作:

Node supports file and buffer operations with the 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'});

这篇关于Base64 Nodejs 中的读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆