PC端 导出后端返回文件流如何实现下载

2026-02-01  本文已影响0人  燕自浩

示例

    const handleExport = async () => {
        try {
            const res = await evaluationApi.value.valuationExport(
                {
                    parkIds: queryParams?.value?.parkIds,
                    searchValue: queryParams?.value?.searchValue,
                    generateStartTime: queryParams?.value?.generateStartTime,
                    generateEndTime: queryParams?.value?.generateEndTime,
                    evaluationType: queryParams?.value?.evaluationType,
                    evaluationStatuses: queryParams?.value?.evaluationStatuses,
                },
                {
                    responseType: 'blob',
                }
            );
            const blob = new Blob([res.data!], { type: 'application/vnd.ms-excel' });
            downloadBlob(`评价看板导出_${dayjs().format('YYYYMMDDHHmmss')}.xls`, blob);
        } catch (error) {
            message.error(`${error}`);
        }
    };



export function downloadBlob(filename: string, blob: Blob) {
    if (isHworkQiankun) {
        const fileURL = window.URL.createObjectURL(blob);
        window.open(fileURL, 'download', filename);
    } else if ((window as any).navigator!.msSaveBlob) {
        (window as any).navigator!.msSaveBlob(blob, filename);
    } else {
        const fileURL = window.URL.createObjectURL(blob);
        const aEl = document.createElement('a');
        aEl.href = fileURL;
        aEl.download = filename;
        document.body.appendChild(aEl);
        aEl.click();
        document.body.removeChild(aEl);
        window.URL.revokeObjectURL(fileURL);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读