HBuilder打包app下載文件問題處理:
問題:剛開始使用<a>標簽的download屬性下載,無法支持IOS,只能在安卓app內下載。
處理方式:使用HTML5+ api 中的Downloader處理
Downloader模塊管理網絡文件下載任務,用於從服務器下載各種文件,並支持跨域訪問操作。
通過plus.downloader獲取下載管理對象。
Downloader下載使用HTTP的GET/POST方式請求下載文件,符合標准HTTP/HTTPS傳輸協議。
downLoadClick(value) { let _this = this; _this.lodingShow = true; let dtask = null; if (~navigator.userAgent.indexOf("Html5Plus")) { let plusReady = function(callback) { if (window.plus) { callback(); } else { document.addEventListener("plusready", callback); } }; plusReady(function() { dtask = plus.downloader.createDownload( value.url, { method: "GET", filename: `_documents/download/${value.fileName}` }, function(d, status) { _this.lodingShow = false; if (status == 200) { _this.alertVal = `<p>文件下載成功:</p>${d.filename}`; _this.showPluginAuto(); plus.runtime.openFile(d.filename); } else { _this.alertVal = "文件下載失敗"; _this.showPluginAuto(); } } ); dtask.start(); }); } }
下載成功后:默認情況下,文件放在了Android/data/io.dcloud.hbuilder/downloads文件夾下面了
iOS : 通常在設備應用沙盒目錄下“/Library/Pandora/documents”
plus.runtime.openFile(d.filename); //
調用相應的默認的第三方程序打開文件
注意:filename保存文件路徑僅支持以"_downloads/"、"_doc/"、"_documents/"開頭的字符串。
文件路徑以文件后綴名結尾(如"_doc/download/a.doc")表明指定保存文件目錄及名稱,以“/”結尾則認為指定保存文件的目錄(此時程序自動生成文件名)。
如果指定的文件已經存在,則自動在文件名后面加"(i)",其中i為數字,如果文件名稱后面已經是此格式,則數字i遞增,如"download(1).doc"。
默認保存目錄為("_downloads"),並自動生成文件名稱。