const downloadFile = (file) => {
    fetch(file.filePath)
        .then(response => {
            if (!response.ok) {
                throw new Error('文件下载失败');
            }
            return response.blob();
        })
        .then(blob => {
            const url = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = url;
            link.download = file.fileName; // 设置下载的文件名
            link.style.display = 'none'; // 隐藏链接
            document.body.appendChild(link);
            link.click(); // 触发点击事件
            document.body.removeChild(link); // 移除 <a> 标签
            window.URL.revokeObjectURL(url); // 释放对象 URL
            message.success('文件下载成功');
        })
        .catch(error => {
            message.error('文件下载失败: ' + error.message);
        });
};

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐