JS远程下载文件流自定义文件名保存本地

const saveBlob = (blob: Blob, filename: string) => {
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', filename); // 自定义文件名和扩展名
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link); // 清理
    window.URL.revokeObjectURL(url); // 释放URL对象
}



 axios.get( url,{responseType: 'blob'}).then((res) => {
        const blob = res.data;
        let name = '文件名'
        saveBlob(blob,name)
    },err=>{
       alert('下载失败')
    })

  

posted @ 2026-01-06 14:06  镇显  阅读(29)  评论(0)    收藏  举报