以下是一个示例,演示如何使用 FileSaver 将 base64 编码的 Excel 文件保存到本地:
yarn add file-saver -d
import * as FileSaver from 'file-saver';// 将 base64 编码的 Excel 文件保存到本地 function saveExcelFile(data) { var blob = b64toBlob(data, 'application/vnd.ms-excel'); saveAs(blob, 'example.xlsx'); } // 将 base64 字符串转换为 Blob 对象 function b64toBlob(b64Data, contentType) { contentType = contentType || ''; var sliceSize = 512; var byteCharacters = atob(b64Data); var byteArrays = []; for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { var slice = byteCharacters.slice(offset, offset + sliceSize); var byteNumbers = new Array(slice.length); for (var i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } var byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } var blob = new Blob(byteArrays, { type: contentType }); return blob; }
标签:slice,FileSaver,base64,excel,blob,offset,var From: https://www.cnblogs.com/huoshengmiao/p/17262344.html