zoukankan      html  css  js  c++  java
  • 网页调用文件另存为js

    查看引用是否正常,页面添加html代码。

    <a id="downLoad" onclick="oDownLoad('downLoad')">下载</a>
        <input type="hidden" name="fgycompany.fileUrl" value="地址" />
    

      

    <script>
            function oDownLoad(obj) {
                alert("dd");
                var odownLoad = document.getElementById(obj);
                var url = $(odownLoad).siblings("input[name='fgycompany.fileUrl']").val();
                //取得浏览器的userAgent字符串
                var userAgent = navigator.userAgent;
                //浏览器类型
                var chromeType = "";
                var isOpera = userAgent.indexOf("Opera") > -1;
                //判断是否Opera浏览器
                if (isOpera) {
                    chromeType = "Opera"
                };
                //判断是否Firefox浏览器
                if (userAgent.indexOf("Firefox") > -1) {
                    chromeType = "FF";
                }
                if (userAgent.indexOf("Chrome") > -1) {
                    chromeType = "Chrome";
                }
                //判断是否Safari浏览器
                if (userAgent.indexOf("Safari") > -1) {
                    chromeType = "Safari";
                }
                //判断是否IE浏览器
                if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
                    chromeType = "IE";
                };
                //判断是否Edge浏览器
                if (userAgent.indexOf("Trident") > -1) {
                    chromeType = "Edge";
                }
                if (chromeType === "IE" || chromeType === "Edge") {
                    //IE、Edge
                    odownLoad.href = "#";
                    var oImg = document.createElement("img");
                    oImg.src = url;
                    oImg.id = "downImg";
                    var odown = document.getElementById("down");
                    odown.appendChild(oImg);
                    var oPop = window.open(document.getElementById('downImg').src, "", "width=1, height=1, top=5000, left=5000");
                    for (; oPop.document.readyState != "complete";) {
                        if (oPop.document.readyState == "complete") break;
                    }
                    oPop.document.execCommand("SaveAs");
                    oPop.close();
                } else {
                    //!IE、Edge
                    odownLoad.href = url;
                    odownLoad.download = "";
                }
            }
        </script>
    

      第二种方法

    var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
                //地址
                save_link.href = "/fonts/glyphicons-halflings-regular.svg";
                save_link.download = name;
                var ev = document.createEvent("MouseEvents");
                ev.initMouseEvent(
                    "click", true, false, window, 0, 0, 0, 0, 0
                    , false, false, false, false, 0, null
                );
                save_link.dispatchEvent(ev);
    

      

  • 相关阅读:
    Microsoft Visual Studio 2005中使用水晶报表
    net的辅助工具列表
    基于.net开发平台项目案例集锦
    一个蛮不错的图形绘制控件 dotnetCHARTING
    统计图控件dotnetCharting和ZedGraph比较
    c#获取真实IP和代理IP
    虚拟主机比较安全设置
    正则表达式 大杂烩
    【转载】说说大型高并发高负载网站的系统架构
    使用.NET实现断点续传
  • 原文地址:https://www.cnblogs.com/jiaxuekai/p/7472232.html
Copyright © 2011-2022 走看看