我使用 jQuery fileDownload 插件从 URL 下载文件。
$.fileDownload(url,{
contentType: "text/csv",
contentDisposition: 'attachment; filename=' +
url.split("/").pop()
})
.done(function(){console.log('successfully downladed')})
.fail(function(){ console.log(`request failed`)});
我什至尝试过使用 JavaScript,但它不起作用
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();
你的 JavaScript 不起作用可能是因为你追加了a
在添加之前添加到正文href
and download
属性。
在触发之前附加click
另请记住,这仅适用于具有同源 URL 的文件(Source https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a).
该属性仅适用于同源 URL https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy.
var a = document.createElement("a");
a.href = url;
fileName = url.split("/").pop();
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)