我們寫程序的時候可能會想用open或者<a>來下載一個txt文件,但我們會發(fā)現(xiàn)我們用瀏覽器下載這個txt文件的時候老是在瀏覽器里面直接打開了,而不是提示下載,這樣的話如果txt文件的內(nèi)容非常多的話肯定是不行的,可能就會導(dǎo)致瀏覽器掛掉,客戶機(jī)器死機(jī)的問題
現(xiàn)整理了個解決辦法,代碼如下:
1.下載頁面
open(action執(zhí)行頁面,'iframeName','');
2.action執(zhí)行頁面
<a id="tempB" style="display:none" href="#" onclick=""> </a>
<iframe width=0 height=0 id="hideIframe" name="hideIframe"></iframe>

<script>
var n=0;

function go(url)
{
n==0?new function()

{
frames("hideIframe").location=url,n=1
}:null;
document.all("hideIframe").readyState!="complete"?setTimeout(go,10):so();
function so()

{
frames("hideIframe").document.execCommand("SaveAs"),n=0
};
}
var tempB = document.getElementById("tempB");
<%

if(path.toLowerCase().indexOf(".txt")>=0)
{
%>

tempB.attachEvent('onclick',function()
{go('<%=request.getContextPath()+"/tempfile/1.txt">');});
<%

}else
{
%>

tempB.attachEvent('onclick',function()
{open('<%=request.getContextPath()+"/tempfile/1.xls">','','');});
<%
}
%>
tempB.click();
</script>