$.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'));

完美解决  但是我不知道为什么  有没有大佬解答

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐