【急急急】大佬们,真心求解答,我用这个方法【uni.downloadFile()】下载apk文件返回400,但是在浏览器中能正常下载
![ccc1zqm](https://img-cdn-tc.dcloud.net.cn/account/identicon/5451cdd0b0d851bb37bf6edbfb799633.png)
- 发布:2023-07-13 09:12
- 更新:2023-07-13 18:21
- 阅读:436
![昭昭L](http://img-cdn-tc.dcloud.net.cn/uploads/avatar/001/57/07/92_avatar_mid.jpg?v=1686905812)
昭昭L - 开心就好
是想做版本更新吗,我把我之前写的发你看看
openAppStore() {
if (plus.os.name == "iOS") {
plus.runtime.openURL("itms-apps://" + 'itunes.apple.com/cn/app/wechat/id6448772558?mt=8');
} else if (plus.os.name == "Android") {
this.download();
}
},
download() {
this.showDownLoad = true
const _this = this;
var dtask = plus.downloader.createDownload(this.filePath, {}, function(d, status) {
// 下载完成
if (status == 200) {
_this.showDownLoad = false // 下载完成再把下载进度弹框关闭即可
plus.runtime.install(plus.io.convertLocalFileSystemURL(d
.filename), {}, {}, function(error) {
uni.showToast({
title: '安装失败',
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败',
duration: 1500
});
}
});
dtask.start();
// 关于进度的获取是使用定时器不断获取已经下载的文件的大小,再对比总大小即可
let timer = setInterval(() => {
let percent = (dtask.downloadedSize / (parseInt(_this.fileSize) || 1)).toFixed(
2) // fileSize文件总大小,后端返回的
_this.percentVal = Math.floor(percent * 100) // 转成整数展示
if (percent >= 1) { // 注意百分比,及时清除定时器即可
clearInterval(timer)
}
}, 18)
}
![Diligent_UI](http://img-cdn-tc.dcloud.net.cn/uploads/avatar/001/28/02/43_avatar_mid.jpg?v=1721956781)
Diligent_UI - 【插件开发】【专治疑难杂症】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=193663(微信搜索飘逸科技UI小程序直接体验)】【骗子请绕道】问题咨询请加QQ群:120594820,代表作灵感实用工具小程序
能具体描述问题吗,提供效果截图和代码截图可以高效率解决问题
![昭昭L](http://img-cdn-tc.dcloud.net.cn/uploads/avatar/001/57/07/92_avatar_mid.jpg?v=1686905812)
昭昭L - 开心就好
完整代码给你你跑跑看,代码肯定没问题的,功能都测试验收过了,还有问题我帮你看看
<template>
<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
<u-popup :show="show" mode="center" :closeOnClickOverlay="false" customStyle="background:none;">
<image src="../../static/ic_huojian.svg" class="bg-image"></image>
<view class="popup-content">
<view class="title">新版本抢先体验啦</view>
<view class="version">{{appVersion}}</view>
<view class="update" v-if="content">更新内容</view>
<scroll-view scroll-y="true" style="height: 200rpx;" v-if="content">
<view class="content">{{content}}</view>
</scroll-view>
<view class="progressBox" v-if="showDownLoad">
<u-loading-icon size="36"></u-loading-icon>
<text class="words">下载中 请勿退出 {{percentVal}}%</text>
</view>
<template v-else>
<u-button type="primary" @click="updateNow" style="margin-top: 42rpx;">立即更新</u-button>
<u-button style="margin-top:12rpx;border:none;" @click="close" v-if="!isForceUpdate">以后再说</u-button>
</template>
</view>
</u-popup>
</template>
<script>
import {
checkAppVersion
} from '@/api/common/index.js'
export default {
name: "UpdateModule",
props: {
auto: {
type: Boolean,
default: true
}
},
data() {
return {
show: false,
showDownLoad: false,
isForceUpdate: false,
appVersion: '', // app版本号
content: '', // 更新的内容
filePath: '', // 文件地址
fileSize: 0, // 文件大小
percentVal: '', // 下载进度
}
},
mounted() {
if (this.auto) {
this.check()
}
},
methods: {
check() {
const _this = this;
this.showDownLoad = false;
this.isForceUpdate = false;
const isShow = uni.getStorageSync('isShowUpdateModuleOpen');
if (isShow && this.auto) {
return
}
if (!_this.auto) {
uni.showLoading({
title: '检查更新中'
})
}
uni.getSystemInfo({
success: function(info) {
checkAppVersion().then(({
data
}) => {
if (data) {
_this.appVersion = data.appVersion;
_this.content = data.appVersionUpdateDetail;
_this.isForceUpdate = data.isUpdate === 1;
_this.filePath = _this.HANDLE_IMAGEURL(data.uploadUrl);
_this.fileSize = data.fileSize;
_this.open()
} else {
if (!_this.auto) {
uni.showToast({
title: '暂无更新',
icon: 'none'
});
}
}
}).finally(() => {
uni.hideLoading()
})
},
});
},
open() {
this.show = true
uni.setStorageSync('isShowUpdateModuleOpen', true)
},
close() {
this.show = false
},
// 立即更新
updateNow() {
// #ifdef APP-PLUS
this.openAppStore()
// #endif
// #ifndef APP-PLUS
this.isForceUpdate = false
// #endif
},
openAppStore() {
if (plus.os.name == "iOS") {
plus.runtime.openURL("itms-apps://" + 'itunes.apple.com/cn/app/wechat/id6448772558?mt=8');
} else if (plus.os.name == "Android") {
// var Uri = plus.android.importClass("android.net.Uri");
// var uri = Uri.parse("market://details?id=" + 'com.tencent.mm');
// var Intent = plus.android.importClass('android.content.Intent');
// var intent = new Intent(Intent.ACTION_VIEW, uri);
// var main = plus.android.runtimeMainActivity();
// main.startActivity(intent);
this.download();
}
},
download() {
this.showDownLoad = true
const _this = this;
var dtask = plus.downloader.createDownload(this.filePath, {}, function(d, status) {
// 下载完成
if (status == 200) {
_this.showDownLoad = false // 下载完成再把下载进度弹框关闭即可
plus.runtime.install(plus.io.convertLocalFileSystemURL(d
.filename), {}, {}, function(error) {
uni.showToast({
title: '安装失败',
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败',
duration: 1500
});
}
});
dtask.start();
// 关于进度的获取是使用定时器不断获取已经下载的文件的大小,再对比总大小即可
let timer = setInterval(() => {
let percent = (dtask.downloadedSize / (parseInt(_this.fileSize) || 1)).toFixed(
2) // fileSize文件总大小,后端返回的
_this.percentVal = Math.floor(percent * 100) // 转成整数展示
if (percent >= 1) { // 注意百分比,及时清除定时器即可
clearInterval(timer)
}
}, 18)
}
}
}
</script>
<style lang="scss" scoped>
.bg-image {
width: 560rpx;
height: 288rpx;
}
.popup-content {
width: 560rpx;
padding: 24rpx 30rpx;
background: #FFFFFF;
border-radius: 26rpx;
border-top-left-radius: 0;
border-top-right-radius: 0;
.title {
text-align: center;
font-size: 32rpx;
font-family: PingFangSC-Semibold, PingFang SC;
font-weight: 600;
color: rgba(0, 0, 0, 0.9);
line-height: 44rpx;
}
.version {
text-align: center;
font-size: 28rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: rgba(0, 0, 0, 0.6);
line-height: 40rpx;
}
.update {
font-size: 28rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: rgba(0, 0, 0, 0.9);
line-height: 40rpx;
}
.content {
font-size: 28rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: rgba(0, 0, 0, 0.6);
line-height: 40rpx;
margin: 8rpx 0 0;
}
}
</style>
![爱tutu爱生活](https://img-cdn-tc.dcloud.net.cn/account/identicon/31392ad09177452b9cd6b5dde8801966.png)
爱tutu爱生活 - 前端渣渣
update() {
// console.log('更新操作');
let _this = this;
var download_T = plus.downloader.createDownload(_this.appData.DownloadUrl, {
retry: 1,
filename: "_doc/update/"
}, function(d, status) {
// console.log("d: ", d);
// plus.nativeUI.closeWaiting();
if (status == "200") {
uni.showToast({
icon: 'loading',
title: "正在安装"
})
plus.runtime.install(d.filename, {
force: true
}, function() {
uni.showToast({
icon: 'success',
title: "安装成功"
})
uni.hideToast();
//安装 wgt 资源包成功后,必须执行 plus.runtime.restart(),否则新的内容并不会生效
setTimeout(() => {
plus.runtime.restart();
}, 300)
}, function(e) {
// console.log(e.message);
uni.showToast({
icon: 'error',
title: "安装失败" + e.message
})
});
} else {
uni.showToast({
icon: 'error',
title: "下载失败"
})
}
})
download_T.addEventListener("statechanged", onStateChanged, false);
function onStateChanged(download) {
// 下载进度
_this.downloadPercent = parseInt(download.downloadedSize / download
.totalSize * 100)
}
download_T.start();
},
ccc1zqm (作者)
update() {
let url = this.authAddress+"/software/agent/download/"+this.fileName;
// url = "http://d8.xiaotongqq.com/com.github.tvbox.osc.tk_289.com.apk";
// url = "https://10.50.1.21/software/agent/download/1.txt";
this.$saveLog("下载地址1:"+url);
我用你了的这个方法,感觉plus.downloader.createDownload都没有执行,不知道为什么,“下载地址2”没有打印出来
2023-07-13 16:50
g***@advantech.com.cn
感谢作者。这个问题困扰我好几天了哈哈。后端返回的APK文件是能下载。但是最后无法解析成绝对路径去匹配这个apk.谢谢 非常感谢
2024-12-30 15:39