$.ajax({ type: 'GET', url: project + "/business/attachment/getDownloadData", data: { id: id }, responseType: 'blob', // 设置响应类型为二进制流 success: function(blob) { // 创建一个链接 var url = window.URL.createObjectURL(new Blob([blob])); // 创建一个隐藏的 <a> 元素 var link = document.createElement('a'); link.style.display = 'none'; link.href = url; link.download = '附件.zip'; // 将链接添加到 DOM 中并模拟点击 document.body.appendChild(link); link.click(); // 释放对象 URL window.URL.revokeObjectURL(url); } });
用ajax怎么下载都下不下来
改成
const url = project + "/business/attachment/getDownloadData"; // 后端接口的 URL
const params = {id: id}; // 请求参数
const queryParams = new URLSearchParams(params).toString();
const fullUrl = url+'?'+queryParams;
fetch(fullUrl)
.then(response => response.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = '附件'+Date.now()+'.zip'; // 下载的文件名
a.click();
window.URL.revokeObjectURL(url);
})
.catch(error => console.error(error))
.finally(()=>$('#dg').datagrid('loaded'));
完美解决 但是我不知道为什么 有没有大佬解答