Base 64は、ピクチャの符号化および復号化を実現する
4651 ワード
フロントエンド開発では、base 64符号化の問題に遭遇することがよくありますが、以前はよく分かりませんでしたが、今日はbase 64符号化とは何かを検討します。
base 64符号化とは?
base 64符号化がどうなっているのかがわかりました。次に、base 64符号化の使用をもっと深く見てみましょう。
const fs = require('fs');
const path = require('path') ;
const Buffer = require('buffer').Buffer;
/**
* @param {String} fileName
*/
function encode_base64(fileName){
console.log(fileName)
fs.readFile(path.join(__dirname,fileName),(err,data)=>{
if(err){
throw err;
}else{
let buf = Buffer.from(data);
let base64 = buf.toString('base64');
fs.writeFile(path.join(__dirname,'voice.txt'),base64,err =>{
if(err) throw err;
});
return base64;
}
})
}
/**
*
* @param {String} base64Str
* @param {String} fileName
*/
function decode_base64(base64Str,fileName){
let data = Buffer.from(base64Str,'base64');
fs.writeFile(path.join(__dirname,fileName),data,err =>{
if(err){
throw err;
}else{
return true;
}
})
}
let pigBaseStr = "iVBORw0KGgoAAAANSUhEUgAAAFwAAAASCAYAAADWvblEAAAG50lEQVR42tWZd4wVRRzH0Z3du4M7RARRREXFih1LYiWWiL2iRkmsscSOBSshsZfYS/Tt7uOOA+OpIWrEggaNJYC9YxcLdkRfO2zr73PMeHPjK8u74w83mey9nZ3ZX/3+vr+5fubKR/5WhYx3Zz7jLcxlvB/l/nUuVA/lQ/+4pGN00O9/cnWGDaNymeCoQqgm50NvWj6jZudD9XqXXqH3k4zf5O9O7jL/rej4nsw/KuOaQuTtm2RHNvaVLGLP/fKRuvY/e/IhEaIohk7KDZn7SgQc726YzGgZUgz9HQqhv01fOCVJkpVKbY3r1bO2GHmHigG/Q95ejdD7AZ36wuDizI/YsxirnbqV7BjaLA9zaYQRb92WxM1Dc5F/lkTEW858ThSOkuygQTU9H/on5sPgGPe5rM8WQu+OepSToAidIPlLxiLkFMVfkEh/VsaT8vsZfhPZYtxfyuoZqqfYsz45gi0LGf+UXEZNYS++0SMYgQz7YwhFxBfjprVl0ViMiPBpI0Te/7LY3jSimlA4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