如何使用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
相关产品推荐
相关产品推荐

