JS はファイル ストリーミングを実装して CSV/Excel/Zip ファイルをダウンロードします

プロジェクトで新しい utils/downloadFile.js を作成し、それを使用するときにインポートできます。

CSVをダウンロード
export function downloadCsv(fileName,data){
    const url = getUrl(data,{});
    cosnt a = document.createElement('a');
    a.href = url;
    a.download = `${fileName}.csv`;
    a.click();
    window.URL.revokeObjectURL(url);

    function getUrl(encoded,options){
        const dataBlob = new Blob([`\ufeff${encoded}`],{type:"text/plain;charset=utf-8"})
        return window.URL.createObjectURl(dataBlob)
    }
}
エクセルをダウンロード
export function downloadExcel(fileName,data){
    const a = document.createElement('a');
    let dataBlob = new Blob([data],{type:"application/x-excel"});
    a.style.display = 'none';
    a.href = URL.createObjectURl(dataBlob);
    a.download = `${fileName}.xls`;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
}
ジップをダウンロード
export function downloadzip(fileName,data){
    const a = document.createElement('a');
    let dataBlob = new Blob([data],{type:"application/zip"});
    a.style.display = 'none';
    a.href = URL.createObjectURl(dataBlob);
    a.download = `${fileName}.zip`;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
}

Blob に対応する特定のより詳細な型は、次の場所に移動できます。

https://www.iana.org/assignments/media-types/media-types.xhtml

おすすめ

転載: blog.csdn.net/m0_65489440/article/details/128915124