我有一个from将图像上传到node.js服务器,我想处理传入的图像数据并将其转换为base64。
我可以使用以下方法在服务器上写出文件:
fs.writeFile('filename.ext', req.files[0].buffer, { encoding: 'ucs2' }, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
但是想做类似的事情
let b64 = fs.readFileSync(req.files[0].buffer, 'ucs2');
console.log(b64); // Outputs string of base-64 text.
我目前在使用此方法时遇到错误:
[ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes
node.js可以实现吗?
我应该考虑什么方法?
这是您的解决方案:
let b64 = fs.readFileSync('the-file-you-want-to-read.here').toString('base64');
console.log(b64);