uni-app,app下载文件到自定义文件夹中,为了方便使用者快速找到下载到手机的文件

html

给一个点击事件'clickOk()'

<view class="sett-item" v-for="(item,index) in manauList" @click="clickOk(item)">
			{{item.original||'未填写手册名称'}}
</view>

js

这里我的app和H5用的是两个方法,app的下载方法是自定义下载路径,H5是用的uni官方方法,下载路径不可以指定位置

clickOk(item) {
				 // #ifdef APP-PLUS 
				// 本地路径开头使用file://,跟上手机文件本地目录storage/emulated/0,
				// 后缀是用于文件命名和格式修改,大家可以使用变量。
				var url = config.baseUrl + item.fileAddress;
				let dtask = plus.downloader.createDownload(url, {
					filename: 'file://storage/emulated/0/云南水投/' + item.original
				}, (d, status) => {
					//d为下载的文件对象
					if (status == 200) {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '已保存到文件夹:/云南水投/'+ item.original, //保存路径
							duration: 3000,
						});
						
						//下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
						let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
						setTimeout(() => {
							plus.runtime.openFile(d.filename); //选择软件打开文件
						}, 1500)
					} else {
						//下载失败
						uni.hideLoading();
						plus.downloader.clear(); //清除下载任务
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '下载失败,请稍后重试',
						});
					}
				})
				dtask.start();
				 // #endif
				 // #ifdef H5
				uni.downloadFile({
					url: config.baseUrl + item.fileAddress,
					success: (res) => {
						if (res.statusCode === 200) {
							console.log(res,'res')
							uni.saveFile({
								// tempFilePath: res.tempFilePath,
								tempFilePath:res.tempFilePath,
								success: function(red) {
									uni.openDocument({
										filePath: red.savedFilePath,
										success: (sus) => {
											console.log('成功打开')
										}
									})
									console.log(red,'123')
								}
							});
							uni.showToast({
								title: '下载成功',
								icon: 'none',
							})
							
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
					},
					complete: function(res) {
						console.log(res)
						if (res.statusCode != 200) {
							uni.showToast({
								title: '下载失败',
								icon: 'error',
							})
						} else {
							this.isshow = 1;
						}
					},
					progress: function(res) {
						uni.showToast({
							title: '下载中,请等待',
							icon: 'none',
						})
					}
				});
				// #endif
			},

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值