我们写程序的时候可能会想用open或者<a>来下载一个txt文件,但我们会发现我们用浏览器下载这个txt文件的时候老是在浏览器里面直接打开了,而不是提示下载,这样的话如果txt文件的内容非常多的话肯定是不行的,可能就会导致浏览器挂掉,客户机器死机的问题
现整理了个解决办法,代码如下:
1.下载页面
open(action执行页面,'iframeName','');
2.action执行页面
<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>