uni-app使用uni-download和uni.saveFile下載保存文件遇到的問題及解決方法
項(xiàng)目場景:
app里的開發(fā)的小程序需要下載文件功能以及一個(gè)下載中心頁面可以查看所有下載的文件,使用了uni.download下載以及uni.saveFile保存文件
下載中心頁面實(shí)現(xiàn)邏輯
1.下載文件后保存文件名和uni.saveFile返回的路徑uni.setStorageSync到緩存里
uni.downloadFile({ method: 'GET', url: 你的url, timeout: 5000, header:{ authorization: 你的token, }, success: (data) => { //接口返回的文件流 console.log("fileData",data) if (data.statusCode === 200) { //文件保存到本地 uni.saveFile({ tempFilePath: data.tempFilePath, //臨時(shí)路徑 success: function(res) { console.log("data.",res) let list = uni.getStorageSync("__local_down_history"); if (list.length) { let arrNew=list let newObj={ path:res.savedFilePath, name:fundcode+"_"+fundNo+'.'+fileType } arrNew.unshift(newObj); _this.localSearchList=arrNew // arrUnique(this.localSearchList); } else { _this.localSearchList = [{ path:res.savedFilePath, name:fundcode+"_"+fundNo+'.'+fileType }]; } console.log("文件緩存",_this.localSearchList) uni.setStorageSync("__local_down_history", _this.localSearchList); } }); } }, fail: (err) => { console.log(err); // uni.showToast({ // icon: 'none', // mask: true, // title: '失敗請重新下載', // }); }, })
2.下載中心讀取uni.getStorageSync緩存的文件列表
<ourLoading isFullScreen :active='loadingStatus' text="加載中..." color='rgb(0, 106, 254)' textColor='rgb(0, 106, 254)' /> <uni-list class="uni-list" :border="false" style="margin-bottom: 50px;"> <!-- 列表渲染 --> <uni-list-item v-for="(item,index) in currnetArr" :key="index" > <template v-slot:body> <view class="main"> <view class="mainContent" > //節(jié)流打開文件 <text class="author" style="color: #89939B;" @tap="$u.throttle(openURL(item.path), 1000)">{{item.name}}</text> </view> </view> </template> </uni-list-item> </uni-list>
data() { return { currnetArr:uni.getStorageSync(你保存的緩存key值), loadingStatus:false, }
openURL(path){ console.log('path',path) const _this = this if(!this.loadingStatus) { this.loadingStatus = true setTimeout(() => { uni.openDocument({ filePath: path, success: function(res) { _this.loadingStatus = false console.log('打開文檔成功'); }, fail: function(e){ _this.loadingStatus = false uni.showToast({ icon: 'none', mask: true, title: '文件打開失敗', }); } }); }, 1000) } },
問題描述
通過這種uni.downloadFile配合uni.saveFile下載并保存文件,然后在下載中心點(diǎn)擊打開文件,邏輯是沒問題的。但是這個(gè)方式蘋果手機(jī)可以正常打開文件,安卓一直打開文件報(bào)錯(cuò)。
原因分析:
分析是download方法的問題,保存文件一般是內(nèi)部復(fù)制,系統(tǒng)差異應(yīng)該是保存路徑的問題, 即uni.saveFile返回的savedFilePath保存的臨時(shí)文件路徑問題
解決方案:
配合H5+的下載方法,強(qiáng)行指定下載路徑,這樣就不會(huì)有臨時(shí)路徑的差異了,從而解決安卓系統(tǒng)打不開文件問題,下面是下載文件最終版本代碼
uni.downloadFile({ method: 'GET', url: 你的url, timeout: 2000, header:{ authorization:你的 token, }, success: (data) => { if (data.statusCode === 200) { plus.io.resolveLocalFileSystemURL( data.tempFilePath, function( entry ) { const name = `${entry.name}.pdf` //這里設(shè)置文件名根據(jù)自己的下載文件后綴來修改,我這邊需求是pdf entry.getParent(function(scb) { entry.moveTo(scb,name, function(sEntry) { //文件保存到本地 uni.saveFile({ tempFilePath: sEntry.fullPath, //臨時(shí)路徑 success: function(res) { // 判斷手機(jī)平臺是 Android 還是 IOS if (uni.getSystemInfoSync().platform === 'android') { uni.showModal({ title:"保存地址為:", content: res.savedFilePath, duration: 3000, }) } else { uni.showModal({ icon: 'none', title: '文件已保存:', content: res.savedFilePath, duration: 3000, }); } let list = uni.getStorageSync("__local_down_history"); if (list.length) { let arrNew=list let newObj={ path:res.savedFilePath, name:"收據(jù)."+ReceiptNo+'.pdf' //這里的保存的name是下載中心展示的文件名,不是這個(gè)文件原本的名字 } arrNew.unshift(newObj); _this.localSearchList=arrNew // arrUnique(this.localSearchList); } else { _this.localSearchList = [{ path:res.savedFilePath, name:"收據(jù)."+ReceiptNo+'.pdf' }]; } console.log("文件緩存",_this.localSearchList) uni.setStorageSync("__local_down_history", _this.localSearchList); } }); } ) }) }) } }, fail: (err) => { console.log(err); uni.showToast({ icon: 'none', mask: true, title: '失敗請重新下載', }); }, })
總結(jié)
到此這篇關(guān)于uni-app使用uni-download和uni.saveFile下載保存文件遇到的問題及解決方法的文章就介紹到這了,更多相關(guān)uni-app下載保存文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實(shí)現(xiàn)屏幕自適應(yīng)局部代碼分享
這篇文章主要介紹了js實(shí)現(xiàn)屏幕自適應(yīng)局部代碼分享,需要的朋友可以參考下2015-01-01bootstrap table表格插件之服務(wù)器端分頁實(shí)例代碼
Bootstrap Table是基于Bootstrap的輕量級表格插件,只需要簡單的配置就可以實(shí)現(xiàn)強(qiáng)大的支持固定表頭、單復(fù)選、排序、分頁、搜索以及自定義表頭等功能。這篇文章主要介紹了bootstrap table表格插件之服務(wù)器端分頁實(shí)例代碼,需要的朋友可以參考下2018-09-09layer.confirm點(diǎn)擊第一個(gè)按鈕關(guān)閉彈出框的方法
今天小編就為大家分享一篇layer.confirm點(diǎn)擊第一個(gè)按鈕關(guān)閉彈出框的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09javascript制作坦克大戰(zhàn)全紀(jì)錄(2)
上文我們簡單的完成了坦克大戰(zhàn)的雛形,本文我們來繼續(xù)完善坦克大戰(zhàn),接下來我們來學(xué)習(xí)制作地圖和碰撞檢測方面的問題。2014-11-11微信小程序仿抖音視頻之整屏上下切換功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序仿抖音視頻之整屏上下切換功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05js常用系統(tǒng)函數(shù)用法實(shí)例分析
這篇文章主要介紹了js常用系統(tǒng)函數(shù)用法,實(shí)例分析了escape、parseInt、parseFloat、isNaN、isFinite等函數(shù)的用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01javaScript實(shí)現(xiàn)復(fù)選框全選反選事件詳解
這篇文章主要為大家詳細(xì)介紹了javaScript實(shí)現(xiàn)復(fù)選框全選反選事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09