base64转blob

        

          export const dataURLtoBlob = (dataurl: string) => {
            const arr: any[] = dataurl.split(',');
            const mime = arr[0].match(/:(.*?);/)[1];
            const bstr = atob(arr[1]);
            let n = bstr.length;
            const u8arr = new Uint8Array(n);
            // eslint-disable-next-line no-plusplus
            while (n--) {
              u8arr[n] = bstr.charCodeAt(n);
            }
            return new Blob([u8arr], { type: mime });
          };
        

base64转file

        

          export const dataURLtoFile = (dataurl: string, filename: string) => {
            const arr: any[] = dataurl.split(',');
            const mime = arr[0].match(/:(.*?);/)[1];
            const bstr = atob(arr[1]);
            let n = bstr.length;
            const u8arr = new Uint8Array(n);
            // eslint-disable-next-line no-plusplus
            while (n--) {
              u8arr[n] = bstr.charCodeAt(n);
            }
            return new File([u8arr], filename, { type: mime });
          };
        

Base64编码并压缩(gzip)的文本 转utf-8文本

        

          const base64GzipToUtf8 = async(base64Data) => {
            try {
              // 1. 将Base64转换为字节数组
              const binaryString = atob(base64Data);
              const bytes = new Uint8Array(binaryString.length);
              for (let i = 0; i < binaryString.length; i++) {
                bytes[i] = binaryString.charCodeAt(i);
              }
              
              // 2. 使用DecompressionStream API解压gzip
              const decompressionStream = new DecompressionStream('gzip');
              const writer = decompressionStream.writable.getWriter();
              writer.write(bytes);
              writer.close();
              
              // 3. 读取解压后的数据
              const decompressedData = await new Response(
                decompressionStream.readable
              ).arrayBuffer();
              
              // 4. 转换为UTF-8文本
              const decoder = new TextDecoder('utf-8');
              return decoder.decode(decompressedData);
            } catch (error) {
              console.error('解压失败:', error);
              throw error;
            }
          }
          base64GzipToUtf8(base64Data)
          .then(text => console.log('转换完成:', text))
          .catch(err => console.error('错误:', err));
        

手机Safari下载pdf,防止在线预览

        

          let blob = 'pdf文件流'
          const file = new File([blob], '名字');
          const objUrl = URL.createObjectURL(file);
          const link = document.createElement('a');
          link.download = 'xxxxx.pdf';
          link.href = objUrl;
          link.click();
          URL.revokeObjectURL(objUrl);
        

复制到剪切板

        

          export const copyWord = (word: string) => {
            const div = document.createElement('div');
            document.body.appendChild(div)
            div.innerText = word
            try {
              let selection: any = window.getSelection()
              let range = document.createRange()
              range.selectNode(div)
              selection.removeAllRanges()
              selection.addRange(range)
              document.execCommand('copy')
              selection.removeAllRanges()
              message.success('复制成功')
            } catch (e) { console.log('你的浏览器不支持复制', e) }
            document.body.removeChild(div);
          }
        

生成Excel 下载

        

          const dataToExcel = (columns: [{title: string, dataIndex: string}], dataSource: {id: number, name: string}) => {
            // 列标题
            let str = '<tr>'
            columns.forEach((item:any) => {
              str += `<th>${item.title}</th>`
            })
            str += '</tr>'
            // 每行加入tr标签,每个单元格加td标签
            for(let i=0; i<dataSource.length; i++;){
              str += '<tr>';
              for(let key in dataSource[i]){
                // 增加\t为了不让表格显示科学计数法或者其他格式
                str+=`<td>${dataSource[i][key] + '\t'}</td>`;     
              }
              str += '</tr>';
            }
            // Worksheet名
            const worksheet = 'Sheet1'
            // 下载的表格模板数据
            const template = `<html
              xmlns:o="urn:schemas-microsoft-com:office:office"
              xmlns:x="urn:schemas-microsoft-com:office:excel"
              xmlns="http://www.w3.org/TR/REC-html40"
            >
              <head>
                <!--[if gte mso 9]>
                  <xml>
                    <x:ExcelWorkbook>
                      <x:ExcelWorksheets>
                        <x:ExcelWorksheet>
                          <x:Name>${worksheet}</x:Name>
                          <x:WorksheetOptions>
                            <x:DisplayGridlines/>
                          </x:WorksheetOptions>
                        </x:ExcelWorksheet>
                      </x:ExcelWorksheets>
                    </x:ExcelWorkbook>
                  </xml>
                <![endif]-->
              </head>
              <body>
                <table>${str}</table>
              </body>
            </html>`;
            const blob = new Blob([template], {type: 'application/vnd.ms-excel'});
            const link = document.createElement('a');
            const href = URL.createObjectURL(blob);
            link.download = '下载表格.xlsx'
            link.href = href;
            link.click();
            URL.revokeObjectURL(href);
          };