【下载文件】uniapp开发小程序,下载文件并保存到本地

25 篇文章 1 订阅

一、第一种方式:(好像在ios系统上不兼容)

1.1实现效果:点击文件附件,下载到本地

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

1.2具体代码:
<view class="contact" @click="downloadFun(item.enclosure)">点击下载</view>
// 下载附件
downloadFun(enclosure){
	var that = this
	//加载框动画
	uni.showLoading({title: '正在下载……'});
				
	// console.log(that.hostUrl + '/uploads'+ enclosure)
	uni.downloadFile({
		url: that.hostUrl + '/uploads'+ enclosure,//下载地址接口返回
		success: (data) => {
			console.log('打印data',data)
			if (data.statusCode === 200) {
				//隐藏加载框
				uni.hideLoading();
				//文件保存到本地
				uni.saveFile({
					tempFilePath: data.tempFilePath, //临时路径
					success: function(res) {
						// console.log('打印res',res)
						uni.showToast({
							icon: 'success',
							mask: true,
							// title: '文件已保存:' + res.savedFilePath, //保存路径
							title: '下载成功' , 
							duration: 2000,
					});
									
				//自动打开文档查看
				setTimeout(() => {
					var filePath = res.savedFilePath;
					uni.openDocument({  //新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
						 filePath: filePath,
						showMenu: true,
						success: function (res) {
							 console.log('打开文档成功');
						}
					});
				}, 2000)
			}
		});
	}
	},
	fail: (err) => {
		console.log(err);
		uni.showToast({
			icon: 'none',
			mask: true,
			title: '文件下载失败',
		});
	},
});
},

———————————————————分割线————————————————————— ———————————————————分割线————————————————————— ———————————————————分割线————————————————————— ———————————————————分割线—————————————————————

二、第二种方式:

2.1:效果图

保存方式是:点击下载按钮,通过微信选择一个好友,发给给好友的方式,进行保存。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.2实现代码:

下载按钮:

	<image @click="downloadsFun(infos.file,infos.id)" src="../../static/images/icon_download@2x.png" mode=""></image>

js:

			// 文件下载预览转发
			downloadsFun(myfile, myid){
				var that = this;
				uni.showLoading({
					title: '正在下载……'
				});
				wx.downloadFile({
				  url:that.hostUrl + myfile, // 下载url
				  // filePath: wx.env.USER_DATA_PATH,
				  success (res) {
					console.log('ressss',res)
					if(res.statusCode == 200) {
						//隐藏加载框
						uni.hideLoading();
						console.log('res.statusCode',res)
						// that.openFileEvs(res)
						wx.shareFileMessage({
							  filePath: res.tempFilePath,
								// fileType: 'xlsx',
							  success(data) {
								console.log('转发成功!!!',data)
								uni.showToast({
									icon: 'success',
									mask: true,
									// title: '文件已保存:' + res.savedFilePath, //保存路径
									title: '转发成功',
									duration: 2000,
								});
							  },
							  // fileName:'自定义文件名字.xlsx',
							  fail: console.error,
						})
					}
				  },
				  // fileName:'导出的文件名',
				  fail: console.error,
				})
			},

ending~

  • 4
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在uniapp下载文件保存到本地,你可以使用两种方法。 方法一:使用uni.downloadFile和uni.saveFile函数。 首先,使用uni.downloadFile函数下载文件,该函数接受一个对象参数,包含文件下载URL。下载成功后,使用uni.saveFile函数将文件保存到本地,该函数也接受一个对象参数,其中的tempFilePath属性指定要保存文件的临时路径。保存成功后,可以使用uni.openDocument函数打开文件。请注意,保存文件位置可能比较奇怪,比如在安卓端可能是"内部存储\Android\data\io.dcloud.HBuilder\apps\HBuilder\doc\uniapp_save"。此外,还要注意保存过程中文件名可能会被篡改。因此,建议使用方法二。 方法二:使用plus.downloader.createDownload和plus.io.convertLocalFileSystemURL函数。 首先,使用plus.downloader.createDownload函数创建一个下载任务,该函数接受两个参数,文件下载地址和文件保存的路径。下载成功后,使用plus.io.convertLocalFileSystemURL函数将保存在本地的相对路径转换为平台绝对路径。然后,使用plus.runtime.openFile函数选择一个软件打开文件。最后,显示一个toast提示文件保存。如果下载失败,可以使用plus.downloader.clear函数清除下载任务。请注意,安卓端文件管理内的路径是file://storage/emulated/0/xx。 下面是一个例子用来下载文件保存到本地: uni.downloadFile({ url: 文件下载路径, success(res) { uni.saveFile({ tempFilePath: res.tempFilePath, success: function(res) { const savedFilePath = res.savedFilePath; uni.openDocument({ filePath: savedFilePath, success: function(res) { uni.hideLoading() }, fail: function(res) {}, complete: function(res) { setTimeout(uni.hideLoading(), 4000) }, }); }, fail: function(err) {} }); }, fail(res) {} }) 请注意替换代码中的"文件下载路径"为实际文件下载URL。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [uniapp 下载文件保存到本地](https://blog.csdn.net/shelter123456/article/details/126038481)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [【下载文件uniapp开发小程序下载文件保存到本地](https://blog.csdn.net/weixin_48596030/article/details/126015687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值