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

html

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


			{{item.original||'未填写手册名称'}}

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
			},

你可能感兴趣的:(uni-app,前端,javascript,vue.js,html,html5)