如何实现微信小程序文件与图片的上传,预览,下载

1.文件上传

                addFile() {
				let that = this;
			if (this.fileType1 == '文件') {
					let that = this;
					uni.chooseMessageFile({
						 count: 3, //默认100,限制上传的数量
						// extension: ['.zip', '.doc', '.docx', '.xlsx', '.xls', '.txt', '.ppt', '.pptx',
						// 	'.pdf'
						// ], //想要上传的文件类型,但是对于部分微信版本,该方法失效,所以可以采用下面的方法判断
						// sourceType: ['camera'],
						success(res) {
							that.resFile = res
							console.log('size33',res);
							var isMaxSize = res.tempFiles[0].size / 1024 / 1024 < 10							
							var type = res.tempFiles[0].name.substring(res.tempFiles[0].name.lastIndexOf('.') + 1)
							let arr = ['jpg', 'jpeg', 'png', 'JPG', 'PNG', 'JPEG', 'doc', 'docx', 'DOC', 'DOCX', 'xls',
								'xlsx', 'XLSX', 'XLS', 'pdf', 'PDF'
							]
							if (arr.indexOf(type) == -1) {
								uni.showToast({
									icon: 'none',
									mask: true,
									title: '请选择正确格式的文件', //保存路径
									duration: 2000,
								});
							} else {
								if(!isMaxSize){
									uni.showToast({
										icon: 'none',
										mask: true,
										title: '上传大小不能超过10MB!', //保存路径
										duration: 2000,
									});
								}else{
									that.upLoadFileByUni(res,'file')
									
								}
							}
							console.log('upload0000000000', res);
					
						},
						complete() {
							console.log('yizhixing');
						}
					});
				} else {
					uni.chooseImage({//使用该方法可以选择相册里的图片,或者直接拍照上传图片
						count: 3,
						sizeType: ['original', 'compressed'],
						sourceType: ['camera', 'album'], //这要注意,camera掉拍照,album是打开手机相册					 
						success(res) {	
							var isMaxSize = res.tempFiles[0].size / 1024 / 1024 < 10
							if(!isMaxSize){
								uni.showToast({
									icon: 'none',
									mask: true,
									title: '上传大小不能超过10MB!', //保存路径
									duration: 2000,
								});
							}else{
								that.upLoadFileByUni(res,'image')
								
							}
						}
					})
			
				}
			
			},
			//上传文件
			upLoadFileByUni(res) {
				let that = this
				uni.uploadFile({
					url: `https://zzygz.512new.com/api/UniFiler/CaseUpload?flag=${this.fileId}&filetype=${this.fileType}`,
					filePath: res.tempFiles[0].path,
					name: 'file',
					formData: { //调用上传接口需要的参数
						type: res.tempFiles[0].type,
						name: res.tempFiles[0].name,//后端需要用它来获取文件的名字
					},
					header: {
						token: uni.getStorageSync('USER_INFO_KEY').token ? uni.getStorageSync('USER_INFO_KEY').token : ''//携带token
					},
					success: (uploadFileRes) => {
						this.getfile(this.fileId)					
						console.log("上传成功", uploadFileRes);
					},
					complete(res) {
						console.log(res);
					}
				})
			},

2.文件的预览

//预览
			preview(item) {				
				uni.downloadFile({
					url: 'https://zzygz.512new.com/api/UniFiler/PcGet?Id=' + item.id, //下载地址接口返回
					header: {
						token: uni.getStorageSync('USER_INFO_KEY').token ? uni.getStorageSync('USER_INFO_KEY')
							.token : ''
					},
					success: (data) => {
						if (data.statusCode === 200) {
							console.log('data', data);
							//文件保存到本地
							wx.getFileSystemManager().saveFile({
								tempFilePath: data.tempFilePath, //临时路径
								success: function(res) {
									console.log('RESyulan', res);
									uni.showToast({
										icon: 'none',
										mask: true,
										title: '加载中', //保存路径
										duration: 2000,
									});
									console.log('000000000000', res.savedFilePath);
									setTimeout(() => {
										//打开文档查看
										uni.openDocument({
											filePath: res.savedFilePath,
											showMenu: true, //右上角是否有可以转发分享的功能
											success: function(res) {
												// console.log('打开文档成功');
											}
										});
									}, 2000)
								}
							});
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '预览失败',
						});
					},
				});
			},

3.文件的下载

	//下载
			down(item) {
				
				uni.downloadFile({
					url: 'https://zzygz.512new.com/api/UniFiler/PcGet?Id=' + item.id, //下载地址接口返回
					header: {
						token: uni.getStorageSync('USER_INFO_KEY').token ? uni.getStorageSync('USER_INFO_KEY')
							.token : ''
					},
					success: (data) => {
						if (data.statusCode === 200) {
							console.log('data11', data);
							if(item.ext == '.jpg'||item.ext=='.jpeg'||item.ext=='.png'||item.ext == '.JPG'||item.ext=='.JPEG'||item.ext=='.PNG'){
								uni.saveImageToPhotosAlbum({
									filePath: data.tempFilePath,
									success: function() {
										uni.showToast({
											title: '已保存至相册',//图片的下载可以用保存到相册
											duration: 2000
										})
								
									},
									fail: function() {
										uni.showToast({
											title: '保存失败',
											duration: 2000
										})
								
									}
								})
							}else{
								wx.getFileSystemManager().saveFile({
									tempFilePath: data.tempFilePath, //临时路径
									success: function(res) {
										console.log('down', res);
										uni.showToast({
											icon: 'none',
											mask: true,
											title: '文件已保存:' + res.savedFilePath, //保存路径
											duration: 2000,
										});
								
										setTimeout(() => {
											//打开文档查看
											uni.openDocument({
												filePath: res.savedFilePath,
												showMenu: true, //右上角是否有可以转发分享的功能,文件的下载可以用此功能转发给别人来实现下载
												success: function(res) {
													// console.log('打开文档成功');
												}
											});
										}, 2000)
									}
								});
							}
							
							
							//文件保存到本地
							
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '下载失败',
						});
					},
				});
			},

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在微信小程序上传文件,你可以使用 `wx.chooseImage` 或 `wx.chooseFile` 方法来上传图片或其他文件。 1. `wx.chooseImage` 方法可以让用户选择图片上传至服务器,代码示例: ```js wx.chooseImage({ count: 1, // 最多可以选择图片张数,默认1张 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePaths可作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths; wx.uploadFile({ url: '服务器地址', // 上传接口地址 filePath: tempFilePaths[0], // 上传文件路径 name: 'file', // 上传文件名称 success: function (res) { // 上传成功后的处理逻辑 } }) } }) ``` 2. `wx.chooseFile` 方法可以让用户选择文件上传至服务器,代码示例: ```js wx.chooseFile({ success(res) { // 选择文件后的处理逻辑 wx.uploadFile({ url: '服务器地址', // 上传接口地址 filePath: res.tempFilePaths[0], // 上传文件路径 name: 'file', // 上传文件名称 success: function (res) { // 上传成功后的处理逻辑 } }) } }) ``` 要预览上传图片文件,可以使用 `wx.previewImage` 方法来预览图片,代码示例: ```js wx.previewImage({ current: '当前图片的url', // 当前显示图片的链接,不填则默认为urls的第一张 urls: ['图片1的url', '图片2的url'] // 需要预览图片链接列表 }) ``` 对于其他类型的文件,可以使用 `wx.openDocument` 方法来打开预览,代码示例: ```js wx.openDocument({ filePath: '文件的本地路径', success: function (res) { // 打开文档成功的处理逻辑 } }) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值