微信公眾號下載文件(避開微信瀏覽器的文件下載方法)


微信瀏覽器是屏蔽資源文件下載的,但是微信公眾號內如何下載文件呢。只能借助於在其他瀏覽器打開,也就是跳到其他瀏覽器進行下載(如圖)。

具體的邏輯很簡單,就是在需要下載的位置比如:是一個單擊事件

<span   onclick="docDownload(filepath)">作業</span>
//filepath為文件的下載訪問路徑   http://dem***o.com/file/2018-12/11/dfd223dfaf.doc
function  docDownload(filepath){
    window.top.location.href="/home/download.html?fileurl="+filepath;
}

這個頁面(/home/download.html)是個引導下載頁(如下效果)

在這個頁面里添加js代碼如下:

//從瀏覽器url地址中獲取參數對應的值
function GetRequest() {
    var url = location.search;//獲取url中"?"符后的字符串
    var theRequest = new Object();
    if (url.indexOf("?")!=-1){  //存在? 則
        var str = url.substr(1);
        strs = str.split("&");  //字符串分割
        for(var i=0;i<strs.length;i++){
            theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
//判斷是否是微信瀏覽器
function is_weixin() {
    var ua = window.navigator.userAgent.toLowerCase(); 
    if (ua.match(/MicroMessenger/i) == 'micromessenger') { 
        console.log('微信瀏覽器');
        return true;
    } else { 
        console.log("不是微信瀏覽器"); 
        return false;
    } 
}

var is_weixin=is_weixin();
if(is_weixin){
        //是微信瀏覽器  顯示返回按鈕   (用戶可以選擇下載或者返回)
        $('#back_btn').show();
    }else{
        //不是微信瀏覽器
        //執行下載
        window.location.href=GetRequest().fileurl;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM