所以我有一个列出文件的表,每个文件的URL都作为数据属性存储在按钮上。在单击按钮时,我想触发一个Javascript / Ajax调用,该调用将在URL下载该文件。
现在有些警告,
到目前为止,我尝试使用XMLHttpRequest:
e.preventDefault();
var csrftoken = getCookie('csrftoken');
var req = new XMLHttpRequest;
req.open("POST",$(this).data("url"));
req.setRequestHeader("MY HEADER", "HEADER VALUE;");
req.withCredentials = true;
req.send();
我也尝试过Ajax GET请求(不强制下载)并在锚标记上使用Download属性(不接受标题)。
由于
答案 0 :(得分:4)
而不是使用不符合跨浏览器标准的锚标签(显然,我没有意识到微软获取HTML5规范的速度太慢了......虽然不出意外)
我们可以利用iframe,避免重定向并在保持跨浏览器合规性的同时实现此效果。
<iframe width="1" height="1" frameborder="0" src="[File location]"></iframe>
只要该文件不是.html或某种可以通过浏览器显示的文件(.htm,.html,.xhtml,.aspx等),它就应该下载该文件。< / p>
答案 1 :(得分:0)
您必须首先捕获网址,然后:
document.location = 'data:Application/octet-stream,' + encodeURIComponent("http://somedomain.com/somefile.some");
或
window.open("http://somedomain.com/somefile.some");
您不需要使用此方法的标题。