base64 image to file

将base64图片保存为文件

1
2
3
4
5
6
const fs = require('fs');
const body = req.rawBody;
const base64Data = body.replace(/^data:image\/png;base64,/,"");
const binaryData = Buffer.from(base64Data, 'base64').toString('binary');

fs.writeFileSync("out.png", binaryData, "binary");

从谷歌附件提取图片并保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const fs = require('fs');
// ....
// callback
function getImage(gmail, id, mid, name) {
gmail.users.messages.attachments.get(
{
userId: 'me',
id: mid,
messageId: id
},
(err, res) => {
if (err) console.log(err);
else {
const image = res.data.data.replace(/_/g, '/').replace(/-/g, '+');
fs.writeFileSync(name, Buffer.from(image, 'base64').toString('binary'), 'binary');
}
}
);
}
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×