添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
f . save ( io_file ) data = io_file . getvalue ( ) base64_data = base64 . b64encode ( data ) #字符串化,使用utf-8的方式解析二进制 base64_str = str ( base64_data , 'utf-8' )
base64_data = base64_str.encode(encoding='utf-8')
data = base64.b64decode(base64_data)

base64转文件下载(方法1)

<script>
  //将base64转换为blob
  function dataURLtoBlob(dataurl) {
    var arr = dataurl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    return new Blob([u8arr], { type: mime });
  // * desc: 下载方法
  // * @param url  :返回数据的blob对象或链接
  // * @param fileName  :下载后文件名标记
  function downloadFile(url, name = "What's the fuvk") {
    var a = document.createElement("a");
    a.setAttribute("href", url);
    a.setAttribute("download", name);
    a.setAttribute("target", "_blank");
    let clickEvent = document.createEvent("MouseEvents");
    clickEvent.initEvent("click", true, true);
    a.dispatchEvent(clickEvent);
  // * desc: 下载参数入口
  // * @param base64  :返回数据的blob对象或链接
  // * @param fileName  :下载后文件名标记
  function downloadFileByBase64(base64, fileName) {
    var myBlob = dataURLtoBlob(base64);
    var myUrl = URL.createObjectURL(myBlob);
    downloadFile(myUrl, fileName);
</script>

注意的点:
bytesio转base64的适合记得要带上header,即mimetype类型

因为不同的文件类型base64前面拼接的不同

//根据文件后缀 获取base64前缀不同
 function getBase64Type(type) {
      switch (type) {
        case 'txt': return 'data:text/plain;base64,';
        case 'doc': return 'data:application/msword;base64,';
        case 'docx': return 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,';
        case 'xls': return 'data:application/vnd.ms-excel;base64,';
        case 'xlsx': return 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,';
        case 'pdf': return 'data:application/pdf;base64,';
        case 'pptx': return 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,';
        case 'ppt': return 'data:application/vnd.ms-powerpoint;base64,';
        case 'png': return 'data:image/png;base64,';
        case 'jpg': return 'data:image/jpeg;base64,';
        case 'gif': return 'data:image/gif;base64,';
        case 'svg': return 'data:image/svg+xml;base64,';
        case 'ico': return 'data:image/x-icon;base64,';
        case 'bmp': return 'data:image/bmp;base64,';
 //附上获取文件后缀的方法  
 function getType(file) {
     var filename = file;
     var index1 = filename.lastIndexOf(".");
     var index2 = filename.length;
     var type = filename.substring(index1 + 1, index2);
     return type;

base64转文件下载(方法2)

// * desc: 下载导出文件
// * @param blob  :返回数据的blob对象或链接
// * @param fileName  :下载后文件名标记
// * @param fileType  :文件类 word(docx) excel(xlsx) ppt等
downloadExportFile(blob, fileName, fileType) {
  const downloadElement = document.createElement('a')
  let href = blob
  if (typeof blob === 'string') {
    downloadElement.target = '_blank'
  } else {
    href = window.URL.createObjectURL(blob) // 创建下载的链接
  downloadElement.href = href
  downloadElement.download = fileName + '.' + fileType // 下载后文件名
  document.body.appendChild(downloadElement)
  downloadElement.click() // 触发点击下载
  document.body.removeChild(downloadElement) // 下载完成移除元素
  if (typeof blob !== 'string') {
    window.URL.revokeObjectURL(href) // 释放掉blob对象
// * desc: base64转文件并下载
// * @param base64 {String} : base64数据
// * @param fileType {String} : 要导出的文件类型png,pdf,doc,mp3等
// * @param fileName {String} : 文件名
downloadFile(base64, fileName, fileType) {
  const typeHeader = 'data:application/' + fileType + ';base64,' // 定义base64 头部文件类型
  const converedBase64 = typeHeader + base64 // 拼接最终的base64
  const blob = this.base64ToBlob(converedBase64, fileType) // 转成blob对象
  this.downloadExportFile(blob, fileName, fileType) // 下载文件
this.downloadFile('你的base64数据','你的文件名称','你的文件数据类型');

上述方法有一定的问题,base64ToBlob方法是缺失的,可以参考方法1中的如何实现,主要干的事就是把base64的数据加上header,方法1的是在后端实现了,而该方法是在前端js中添加的,只不过方法并没有写,其实就是拼接。

functiondataURLtoBlob(dataurl){ vararr=dataurl.split(','),mime=arr[0].match(/:(.*?);/)[1], bstr=atob(arr[1]),n=bstr.length,u8arr=newUint8Array(n); while... <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>base64编码图片解析</title> <script type="text/javascript"> functi...
let base64Str = "data:image/jpeg;base64,"+base64; let aLink = document.createElement("a"); aLink.style.display = "none"; aLink.href = base64Str; aLink.download = "test.jpg"; // 触发点击-然后移除 document.body.appendChild(aLink); aLink.click(); document.body.remov.
前端实现Base64编码的数据换为文件保存本地,通常会经历以下几个步骤: 1. 获取Base64编码的数据[^1],这通常是通过WebGL录音源码捕获的音频数据,然后化为字符串形式。 ```javascript // 假设audioData是录音后的Base64编码 const audioDataURL = "your_base64_data_here"; 2. 使用`atob()`函数解码Base64字符串,得到原始二进制数据。 ```javascript const decodedData = atob(audioDataURL); 3. 创建一个新的Blob对象,该对象代表了存储在内存中的文件数据。 ```javascript let binaryData = new Uint8Array(decodedData.length); for (let i = 0; i < decodedData.length; i++) { binaryData[i] = decodedData.charCodeAt(i); const blob = new Blob([binaryData], {type: "audio/mpeg"}); 4. 创建一个URL.createObjectURL()来创建临时的URL,以便于后续下载操作[^2]。 ```javascript const url = URL.createObjectURL(blob); 5. 创建HTML链接元素用于触发浏览器的下载行为。 ```javascript const aLink = document.createElement('a'); aLink.href = url; aLink.download = 'filename.mp3'; // 文件名可根据需要设置 document.body.appendChild(aLink); aLink.click(); 6. 当下载完成时,记得删除创建的临时URL,以释放资源。 ```javascript window.URL.revokeObjectURL(url); 这样就实现了从Base64数据到本地文件下载。注意,如果数据不是常见的MIME类型(如audio/mp3),可能需要指定正确的MIME类型。 python tkinter 组件功能实例总结(代码+效果图)(Radiobutton | Button | Entry | Menu | Text) abc12321abcbac: 非常好,学习了 Vector CANape - How to Send Receive CAN Message in CANape 郭郭大侠: 大哥两年过去了,现在您会外发报文了嘛 Python Install 报错”‘SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] Linux Centos装机全过程总结,小白入(持续更新中...) 汽车行业DBC文件解析 | Python 解析dbc文件