Buffer
(logo.png)
以下代码读取logo.png为buffer类型
然后将buffer转化为string
可以将字符串配置: data:image/png;Base64,Buffer.toString()
var fs = require(‘fs‘)
fs.readFile(‘logo.png‘,function(err,origin_buffer){
console.log(‘origin_buffer是否Buffer格式:‘+Buffer.isBuffer(origin_buffer))
//console.log(‘读取到origin_buffer:‘ + origin_buffer)
//创建文件
fs.writeFile(‘log_buffer.png‘,origin_buffer,function(err){
if(err) console.log(err)
})
//var base84Image = new Buffer(origin_buffer).toString(‘base64‘)
//转为字符串
var base64Image = origin_buffer.toString(‘base64‘)
console.log(‘字符串base64Image:‘ + base64Image)
var decodedImage = new Buffer(base64Image, ‘base64‘)
//console.log(‘编码decodedImage:‘+decodedImage)
//console.log(‘比较origin_buffer和decodedImage:‘ + Buffer.compare(origin_buffer,decodedImage))
fs.writeFile(‘logo_decoded.png‘,decodedImage,function(err){
if(err) console.log(err)
})
})
原文地址:https://www.cnblogs.com/-beauTiFul/p/9200760.html
时间: 2024-11-05 19:44:42