zoukankan      html  css  js  c++  java
  • base64 转 File

    1.通过new File()将base64转换成file文件,此方式需考虑浏览器兼容问题。
    // 将base64转换为文件
    function dataURLtoFile(dataurl, filename) {
      let 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 File([u8arr], filename, { type: mime });
    }
    
    //调用
    let file = dataURLtoFile(base64Data, imgName);
    2.先将base64转换成blob,再将blob转换成file文件,此方法不存在浏览器不兼容问题。
    // 将base64转换为blob
    function dataURLtoBlob(dataurl) {
      let 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 });
    }
    // 将blob转换为file
    function blobToFile(theBlob, fileName) {
      theBlob.lastModifiedDate = new Date();
      theBlob.name = fileName;
      return theBlob;
    }
    // 调用
    let blob = dataURLtoBlob(base64Data);
    let file = blobToFile(blob, imgName);
    .
  • 相关阅读:
    中风后遗症
    慢性湿疹半年
    女子脚背痒肿案
    肾盂肾炎病案
    鼻衄二则
    糖尿病病案
    慢性肠炎2例
    子宫肌瘤病案2例
    眩晕病案
    前列腺炎病案3例
  • 原文地址:https://www.cnblogs.com/crazycode2/p/13272212.html
Copyright © 2011-2022 走看看