1
我试图让我的下载功能弹出与文件下载,它是输出文件的二进制文件到div当我点击链接,我如何使它,以便当我点击链接,它会问我下载它?我知道我可以使用querystring并在PHP中使用标题,但我可以用ajax/javascript以类似的方式做到这一点吗?谢谢,这是我的尝试:如何从javascript下载文件
<html>
<head>
<script>
function Download(plan_name)
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var resp = xmlhttp.responseText;
document.getElementById("txtHint").innerHTML=resp;
//HOW TO SHOW test.zip in a save-as dialog?
}
}
xmlhttp.open("GET","fetcher.php?file=/raid0/data/naswebsite/Projects/Projects/07-003_Dawson_Mine/Flight\ Plans/Dawson_Sth_1211_AMG_700.zip");
xmlhttp.send();
}
</script>
</head>
<body>
<a href="#" onClick="Download();">Test Download</a>
<div id="txtHint"></div>
</body>
</html>
或者做大多数人做的事情,用'window.open'打开一个新窗口并设置它的目标URL,并且使用头部Content-Disposition:attachment; filename = whatever和/或'Content-Type:application/force-download' – Ian