我使用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();
答案 0 :(得分:1)
您的JavaScript无效,可能是因为您在添加a
和href
属性之前将download
附加到正文。
在触发click
另请注意,这仅适用于具有相同来源网址(Source)的文件。
此属性仅适用于same-origin URLs。
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();