企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
#### 难度:阿里p5 ~ p6 ~~~ const fs = require('fs'); const path = 'xxx/'+ Date.now() +'.png'; //去掉图片base64码前面部分data:image/png;base64 const base64 = data.replace(/^data:image\/\w+;base64,/, ""); //把base64码转成buffer对象, const dataBuffer = new Buffer(base64, 'base64'); fs.writeFile(path, dataBuffer, function(err){//用fs写入文件 if(err){ console.log(err); }else{ console.log('写入成功!'); } }) ~~~ 一般你要是做出来了将base64转化成png文件,面试官又会坏坏的让你举一反三一下,会问你:实现一下将png文件,转化成base64, ~~~ const fs = require("fs"); const util = require("util"); // 例:xxx/xx/xx.png const imageData = await util.promisify(fs.readFileSync(fileUrl)); const imageBase64 = imageData.toString("base64"); const imagePrefix = "data:image/png;base64,"; console.log(imagePrefix + imageBase64); ~~~ ## 摘自 [ 用nodejs,将base64转化成png文件](https://juejin.im/post/5d46f433e51d4561ca2121fc#heading-15)