• base64 转 file / blob

      1. const base64 = '';
      2. let arr = base64.split(','),
      3. mime = arr[0].match(/:(.*?);/)[1],
      4. bstr = atob(arr[1]),
      5. n = bstr.length,
      6. u8arr = new Uint8Array(n);
      7. while(n--) {
      8. u8arr[n] = bstr.charCodeAt(n);
      9. }
      10. const file = new File([u8arr], 'file.name', {type: mime}); // file
      11. const blob = new Blob([u8arr], {type: mime}); // blob
    • file / blob 转 base64

      1. function change(file) {
      2. return new Promise((resolve, reject) => {
      3. const reader = new FileReader();
      4. reader.onload = e => {
      5. resolve(e.target.result);
      6. }
      7. reader.onerror = error => {
      8. resolve(error);
      9. }
      10. reader.readAsDataURL(file);
      11. })
      12. }
      13. const base64 = change(file);
    • blob 转 file

      1. function change(blob, fileName) {
      2. blob.lastModifiedDate = new Date();
      3. blob.name = fileName;
      4. return blob;
      5. }
      6. const file = change(blob, fileName);