項(xiàng)目場(chǎng)景:
app里的開(kāi)發(fā)的小程序需要下載文件功能以及一個(gè)下載中心頁(yè)面可以查看所有下載的文件,使用了uni.download下載以及uni.saveFile保存文件
下載中心頁(yè)面實(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: '失敗請(qǐng)重新下載',
// });
},
})
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é)流打開(kāi)文件
<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('打開(kāi)文檔成功');
},
fail: function(e){
_this.loadingStatus = false
uni.showToast({
icon: 'none',
mask: true,
title: '文件打開(kāi)失敗',
});
}
});
}, 1000)
}
},
問(wèn)題描述
通過(guò)這種uni.downloadFile配合uni.saveFile下載并保存文件,然后在下載中心點(diǎn)擊打開(kāi)文件,邏輯是沒(méi)問(wèn)題的。但是這個(gè)方式蘋果手機(jī)可以正常打開(kāi)文件,安卓一直打開(kāi)文件報(bào)錯(cuò)。
原因分析:
分析是download方法的問(wèn)題,保存文件一般是內(nèi)部復(fù)制,系統(tǒng)差異應(yīng)該是保存路徑的問(wèn)題, 即uni.saveFile返回的savedFilePath保存的臨時(shí)文件路徑問(wèn)題文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-526045.html
解決方案:
配合H5+的下載方法,強(qiáng)行指定下載路徑,這樣就不會(huì)有臨時(shí)路徑的差異了,從而解決安卓系統(tǒng)打不開(kāi)文件問(wèn)題,下面是下載文件最終版本代碼文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-526045.html
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ù)自己的下載文件后綴來(lái)修改,我這邊需求是pdf
entry.getParent(function(scb) {
entry.moveTo(scb,name, function(sEntry) {
//文件保存到本地
uni.saveFile({
tempFilePath: sEntry.fullPath, //臨時(shí)路徑
success: function(res) {
// 判斷手機(jī)平臺(tái)是 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: '失敗請(qǐng)重新下載',
});
},
})
到了這里,關(guān)于實(shí)際記錄uni-app使用uni-download和uni.saveFile下載保存文件遇到的問(wèn)題以及解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!