Js下载文件到本地(兼容多浏览器)

在客户端通过js下载文件,试过几种下载方式,iframe方式仅限于IE浏览器,window.open(url),location.href=url 这两种方式在chrome浏览器还会是直接打开文件而不是下载,百度N久没有结果,在谷歌还是找到答案了,下载链接在此。

window.downloadFile = function (sUrl) {

    //iOS devices do not support downloading. We have to inform user about this.
    if (/(iP)/g.test(navigator.userAgent)) {
        alert(‘Your device does not support files downloading. Please try again in desktop browser.‘);
        return false;
    }

    //If in Chrome or Safari - download via virtual link click
    if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
        //Creating new link node.
        var link = document.createElement(‘a‘);
        link.href = sUrl;

        if (link.download !== undefined) {
            //Set HTML5 download attribute. This will prevent file from opening if supported.
            var fileName = sUrl.substring(sUrl.lastIndexOf(‘/‘) + 1, sUrl.length);
            link.download = fileName;
        }

        //Dispatching click event.
        if (document.createEvent) {
            var e = document.createEvent(‘MouseEvents‘);
            e.initEvent(‘click‘, true, true);
            link.dispatchEvent(e);
            return true;
        }
    }

    // Force file download (whether supported by server).
    if (sUrl.indexOf(‘?‘) === -1) {
        sUrl += ‘?download‘;
    }

    window.open(sUrl, ‘_self‘);
    return true;
}

window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf(‘chrome‘) > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf(‘safari‘) > -1;
时间: 2024-08-10 17:01:25

Js下载文件到本地(兼容多浏览器)的相关文章

两种 js下载文件的方法(转)

function DownURL(strRemoteURL, strLocalURL){ try{ var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP"); xmlHTTP.open("Get", strRemoteURL, false); xmlHTTP.send(); var adodbStream = new ActiveXObject("ADODB.Stream"); adodbStrea

php中关于js保存文件至本地的问题

最近在搞一个livezilla的在线客服聊天的东东,客户界面要求添加一个下载聊天记录的功能.于是我就是翻看了下网上的各种关于”js保存文件至本地“的资料,发现只能在IE下通过execCommand实现.于是又是一番折腾啊.言归正传,下面开始上正餐. html标签 <td <!--FU_HIDDEN-->><a id="download" target="_blank" ><img class="lz_chat_cl

PHP CURL实现远程下载文件到本地

<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($result); function httpcopy($url,$file='',$timeout=60){ $file=empty($file)?pathinfo($url,PATHINFO_BASENAME):$file; $dir=pathinfo($file,PATHINFO_DIRNAME); !i

js 下载文件 修改文件名

用js下载文件,使用<a>标签,添加download属性即可. var a = document.createElement("a"); a.href = "http://XXX.com/audioStream/8a9dbae9d0859e48fc1f590fcf6d4ccc.mp3": a.download ="test.mp3"; a.click(); 但是如果想给文件重新命名,貌似js无法实现. 因此考虑后台实现,用java代理

js 保存文件到本地

原文http://blog.163.com/[email protected]/blog/static/87727415201310975054613/ js 保存文件到本地 2013-11-09 19:56:35| 分类: 默认分类 |举报|字号 订阅 var obj_target = document.createElementNS('http://www.w3.org/1999/xhtml', 'a'); if(obj_target)//非ie { obj_target.href = ‘1

c#.net从ftp下载文件到本地

c#.net从ftp下载文件到本地    /*首先从配置文件读取ftp的登录信息*/ string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString(); string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName&q

javascript创建css、js,onload触发callback兼容主流浏览器的实现

http://www.fantxi.com/blog/archives/load-css-js-callback/ 由于需要写个函数,既可以加载css,又可以加载js,所以对各主流浏览器对加载js.css后是否触发onload事件做了个测试.当然,为了兼容,首先要考虑的是会用到onload和onreadystatechange,但他们并不是万能的.加载js文件onload触发callback不算大问题.css比较特殊,因为Webkeit/FF下加载css不会触发onload事件.所以研究了一晚上

js下载文件

function DownURL(strRemoteURL, strLocalURL){ try{ var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP"); xmlHTTP.open("Get", strRemoteURL, false); xmlHTTP.send(); var adodbStream = new ActiveXObject("ADODB.Stream"); adodbStrea

本地上传文件到服务器,从服务器下载文件到本地

最近在做项目的时候涉及到了文件的上传.下载,以前学习IO时也没有搞得多清楚,在网上找了些上传下载的例子,然后修改了部分.经测试,上传下载文件暂时能用,下面是上传和下载的方法: 1.本地上传文件到服务器 html代码: <form id="uploadDatumInfo" name="uploadDatumInfo" method="post" enctype="multipart/form-data" target=&q