本文主要是介绍导出不用a便签重复造轮子,file-saver 导出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
利用第三方插件file-saver
yarn add -s file-saver
文件流
import * as FileSaver from 'file-saver'; // 引用export function onFileSaver(data: any, name: any) { //data 文件流 , name 文件名const blob = b64toBlob(data, 'application/vnd.ms-excel');FileSaver.saveAs(blob, `${name}.xlsx`);}export function b64toBlob(b64Data: string, contentType: string) {contentType = contentType || '';const sliceSize = 512;const byteCharacters = atob(b64Data);const byteArrays = [];for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {const slice = byteCharacters?.slice(offset, offset + sliceSize);const byteNumbers = new Array(slice.length);for (let i = 0; i < slice.length; i++) {byteNumbers[i] = slice.charCodeAt(i);}const byteArray = new Uint8Array(byteNumbers);// @ts-ignorebyteArrays.push(byteArray);}const blob = new Blob(byteArrays, { type: contentType });return blob;}
调用onFileSaver
这篇关于导出不用a便签重复造轮子,file-saver 导出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!