我正在尝试从客户端读取以 base64 编码的图像。
如何使用nodejs进行阅读?
My code:
// 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.]
最新且最好的方法:
节点支持文件和缓冲区操作base64编码 https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings:
const fs = require('fs');
const contents = fs.readFileSync('/path/to/file.jpg', {encoding: 'base64'});
或者使用新的承诺API https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options:
const fs = require('fs').promises;
const contents = await fs.readFile('/path/to/file.jpg', {encoding: 'base64'});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)