You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript或jQuery从URL下载文件?

我尝试过的文件下载实现方案

我之前试过用两种方式从URL下载文件,但都没能正常运行。

使用jQuery fileDownload插件的方法

这是我用插件写的代码:

$.fileDownload(url,{ 
  contentType: "text/csv", 
  contentDisposition: 'attachment; filename=' + url.split("/").pop() 
}) 
.done(function(){
  console.log('successfully downladed')
}) 
.fail(function(){ 
  console.log(`request failed`)
});

使用原生JavaScript的方法

后来我还尝试了原生JS的实现,同样没成功:

var a = document.createElement("a"); 
document.body.appendChild(a); 
a.href = url; 
fileName = url.split("/").pop(); 
a.download = fileName;
a.click(); 
window.URL.revokeObjectURL(url); 
a.remove();

内容的提问来源于stack exchange,提问作者bhasker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:45:47