最近接到一個(gè)項(xiàng)目需求,下載各種格式文件保存到手機(jī)本地
遇到的問題如下:
1、iphone手機(jī)無法保存到文件中
2、Android手機(jī)文件保存的位置不易查找
3、Android手機(jī)文件存儲名稱非文件原名,而是以時(shí)間戳命名
不可抗因素:
?1、iphone自帶的文件管理功能不能自動掃描各個(gè)APP下載的文件,需要手動保存一次才可以在文件管理器中找到
2、小程序?qū)ο螺d文件不友好,圖片、視頻可以正常下載到相冊。其他格式文件Android手機(jī)只能下載其規(guī)定的文件夾中,iphone手機(jī)下載后無法無法保存
解決方法:文章來源:http://www.zghlxwxcb.cn/news/detail-504189.html
直接上代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-504189.html
<template>
<view>
<view class="linView">
<view class="load-progress" :class="loadProgress!=0?'show':'hide'" :style="[{top:CustomBar+'px'}]">
<view class="load-progress-bar bg-green" :style="[{transform: 'translate3d(-' + (100-loadProgress) + '%, 0px, 0px)'}]"></view>
<view class="load-progress-spinner text-green"></view>
<view class="dltDownLv">
<view>正在為您下載</view>
<view class="dltDownLvNew">{{dltDownLvNew}}</view>
<view>/</view>
<view>{{dltDownLvAll}}</view>
<view class="dltDownLvWc">已完成:</view>
<view>{{dltDownLvWc}}</view>
</view>
</view>
</view>
<view @tap="clickPeople('下載地址')">下載</view>
</view>
</template>
<script>
export default{
data(){
return{
loadProgress: 0,//加載index
CustomBar: this.CustomBar,
dltDownLvNew:"",//已下載
dltDownLvAll:"",//總長度
dltDownLvWc:"",//完成率
downloadUlr:"",//下載地址
suffix:"",//文件后綴
}
},
methods:{
clickPeople(e){//點(diǎn)擊下載
let _this = this;
//下載地址
this.downloadUlr = e;
//獲取地址后綴
this.suffix = e.split(".")[e.split(".").length - 1];
//判斷是否為(圖片或視頻)
if(e.substring(e.length - 3) == "MP4" || e.substring(e.length - 3) == "mp4" || e.substring(e.length - 3) == "jpg" || e.substring(e.length - 3) == "png"){
const downloadTask = uni.downloadFile({
url:e,
success: res => {
if (res.statusCode === 200) {
if(res.tempFilePath.substring(res.tempFilePath.length - 3) == "mp4" || res.tempFilePath.substring(res.tempFilePath.length - 3) == "MP4"){//視頻
//保存視頻到相冊
uni.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success: function () {
uni.showToast({
title: '保存成功',
icon: 'none',
duration:2000,
mask:true
});
},
fail: function() {
this.loadProgress = 0;
uni.showToast({
title: '保存失敗',
icon: 'none'
});
}
});
}else{//圖片
// 圖片保存到手機(jī)相冊
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function() {
uni.showToast({
title: '保存成功',
icon: 'none',
duration:2000,
mask:true
});
},
fail: function() {
this.loadProgress = 0;
uni.showToast({
title: '保存失敗',
icon: 'none'
});
}
});
}
}else{
uni.showToast({
title:'下載失敗',
icon:"none"
})
}
},
fail:(err) => {
this.loadProgress = 0;
uni.showToast({
icon:"none",
mask:true,
title:'下載失敗'
})
}
});
downloadTask.onProgressUpdate((res) => {
this.loadProgress = res.progress;
if (this.loadProgress < 100) {
} else {
this.loadProgress = 0;
}
if(res.totalBytesExpectedToWrite < 10485760){
this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1024) + "KB";
this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1024) + "KB";
this.dltDownLvWc = res.progress + "%"
}else{
this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1048576) + "M"
this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1048576) + "M";
this.dltDownLvWc = res.progress + "%"
}
});
}else{
//下載文件為非圖片和視頻的文件
let _this = this;
const downloadTaskt = uni.downloadFile({
url:e,//下載地址接口返回
success:(data) => {
uni.hideLoading()
if(data.statusCode === 200){
var sFilePath = data.tempFilePath
var sFileName = _this.downloadUlr.split('/')[_this.downloadUlr.split('/').length - 1];//原來的文件名
//#ifdef APP-PLUS
//文件保存到本地
uni.saveFile({
tempFilePath: data.tempFilePath,//臨時(shí)路徑
success:function(res){
var savedFilePath = res.savedFilePath;
let osname = plus.os.name;
//ios手機(jī)直接打開文件,手動存儲文件到手機(jī),Android手機(jī)從根目錄創(chuàng)建文件夾,保存文件并改名
if (osname == 'Android') {
uni.showToast({
icon:'none',
mask:true,
title:'保存成功',
duration:1000,
});
_this.fSetFileName(res.savedFilePath, sFilePath);
}
setTimeout(() => {
//打開文檔查看
uni.openDocument({
filePath:res.savedFilePath,
success:function(res){
// console.log("成功打開文件")
},
fail(){
// console.log("打開文件失敗")
}
})
},1000)
},
fail:function(res){
}
});
//#endif
//#ifdef MP-WEIXIN
//小程序?qū)ξ募螺d并不友好,不建議使用小程序當(dāng)做下載器
const FileSystemManager = wx.getFileSystemManager();
FileSystemManager.saveFile({//下載成功后保存到本地
tempFilePath:data.tempFilePath,
filePath:wx.env.USER_DATA_PATH + "/" + sFileName,
success(res2){
if(res2.errMsg == 'saveFile:ok'){
// 判斷手機(jī)平臺是 Android 還是 IOS
if (uni.getSystemInfoSync().platform === 'android') {
// console.log('運(yùn)行Android上')
uni.showModal({
title:"保存地址為",
content:"手機(jī)存儲/Android/data/com.tencent.mm/MicroMsg/wxanewfiles"
})
} else {
// console.log('運(yùn)行iOS上')
uni.showToast({
title:"請轉(zhuǎn)移APP下載",
icon:"none"
})
}
}else{
uni.showToast({
title:"下載失敗",
icon:"none"
})
}
},
fail(){
uni.showToast({
title:"下載失敗",
icon:"none"
})
}
})
//#endif
}else{
this.loadProgress = 0;
uni.showToast({
icon:"none",
mask:true,
title:"下載失敗"
})
}
},
fail:(err) => {
this.arcZzShow = false;
this.loadProgress = 0;
uni.showToast({
icon:"none",
mask:true,
title:"下載失敗"
})
}
})
downloadTaskt.onProgressUpdate((res) => {
this.loadProgress = res.progress;
if (this.loadProgress < 100) {
} else {
this.loadProgress = 0;
}
if(res.totalBytesExpectedToWrite < 10485760){
this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1024) + "KB";
this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1024) + "KB";
this.dltDownLvWc = res.progress + "%"
}else{
this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1048576) + "M"
this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1048576) + "M";
this.dltDownLvWc = res.progress + "%"
}
});
}
},//點(diǎn)擊下載END
// 給下載的文件重命名
fSetFileName(sFilePath, sFileName) {
var sFileName = sFileName.split('/')[sFileName.split('/').length - 1];//原來的文件名
var aFileUrl = sFilePath.split('/').splice(0, sFilePath.split('/').length - 1);//saveFile API保存的本地地址
var url = this.downloadUlr;//下載地址
// 'url下載地址(和上面的一樣)'
let dtask = plus.downloader.createDownload(url, {
filename: "file://storage/emulated/0/AAA/" + sFileName //在手機(jī)存儲更目錄創(chuàng)建一個(gè)叫AAA的文件夾,把文件存儲進(jìn)去,并更改會原名
},
(d, status) => {
if (status == 200) {
let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
} else {
//下載失敗
plus.downloader.clear(); //清除下載任務(wù)
}
})
dtask.start();
}
}
}
</script>
<style>
.load-progress {
pointer-events: none;
top: 0;
position: absolute;
width: 100%;
left: 0;
z-index: 2000;
}
.load-progress.hide {
display: none;
}
.load-progress .load-progress-bar {
position: relative;
width: 100%;
height: 2px;
overflow: hidden;
-webkit-transition: all 200ms ease 0s;
transition: all 200ms ease 0s;
}
.bg-green {
background-color: #ff692d;
color: #ffffff;
}
.load-progress .load-progress-spinner {
position: absolute;
top: 5px;
right: 5px;
z-index: 2000;
display: block;
}
.text-green, .line-green, .lines-green {
color: #ff692d;
}
</style>
到了這里,關(guān)于uniapp下載文件保存到手機(jī)本地的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!