1. // 'data:application/apollo-zstd;version=1.1.1;base64,' 注意一下,后端返回的base64
    2. export function base64ToBlob(base64: string): Blob {
    3. if (!base64) {
    4. return;
    5. }
    6. const mimeString = base64.split(',')[0]
    7. .split(':')[1]
    8. .split(';')[0];
    9. const byteString = atob(base64.split(',')[1]);
    10. const arrayBuffer = new ArrayBuffer(byteString.length);
    11. const intArray = new Uint8Array(arrayBuffer);
    12. for (let i = 0; i < byteString.length; i++) {
    13. intArray[i] = byteString.charCodeAt(i);
    14. }
    15. return new Blob([intArray], {type: mimeString});
    16. }
    1. public dataURLtoFile(dataurl: any, filename: any): any {
    2. const arr = dataurl.split(',');
    3. const mime = arr[0].match(/:(.*?);/)[1];
    4. const bstr = atob(arr[1]);
    5. let n = bstr.length;
    6. const u8arr = new Uint8Array(n);
    7. while (n--) {
    8. u8arr[n] = bstr.charCodeAt(n);
    9. }
    10. return new File([u8arr], filename, { type: mime });
    11. }
    1. const blob = new Blob([this.file.nativeElement.files[0]], { type: '' });
    2. const formate = new FormData(); // 通过append(key,value)来添加数据
    3. this.file.nativeElement.value = '';// 用于多次上传,要清空之前的值
    1. const aBlob = new Blob( array, options );
    2. export function downLoadFile(data: ArrayBuffer, fileNmame: string, type: string, fileType: string = 'xls'): void {
    3. const blob = new Blob([data], { type });
    4. const url = window.URL.createObjectURL(blob);
    5. const eleLink = document.createElement('a');
    6. eleLink.download = `${fileNmame}.${fileType}`;
    7. eleLink.style.display = 'none';
    8. eleLink.href = url;
    9. // 触发点击
    10. document.body.appendChild(eleLink);
    11. eleLink.click();
    12. // 然后移除
    13. document.body.removeChild(eleLink);
    14. }