uniapp/vue中将base64图片转为文件

效果图:

uniapp/vue中将base64图片转为文件_第1张图片

实现功能:将签字返回的base64图片转为文件

 1.绑定input


				
			

2.给事件传参,进行打印结果

			// 上传签名图片base64
			input(e) {
				var that = this
				console.log(e, '里面有什么');
				let timestamp = new Date().getTime();
				let sunumber = Math.floor(Math.random() * 999);
				var file = this.base64ToFile(e, timestamp + sunumber)
				console.log(file, '转换完的file');
				uni.uploadFile({
					url: that.imgbaseUrl + "sys/common/upload",
					name: 'file',
					file: file,
					fileType: 'image',
					formData: {
						'user': 'test' // 上传附带参数
					},
					success: (uploadFileRes) => {
						console.log("上传照片res", uploadFileRes)
						console.log('message', uploadFileRes.data.message);
						let path = JSON.parse(uploadFileRes.data).message
						console.log(path, "上传图片之后");
						that.uploadSign = path
						//20230728093836_1690508316394.jpg
						// that.preImgUrl.push(path)
						// uni.$emit('image', that.preImgUrl.join(','))
					},
					fail: (res) => {
						console.log('上传失败', res)
					},
				})
				// this.signimage = baseString.name
			},
			//base64转flie
			base64ToFile(base64, name) {
				if (typeof base64 != 'string') {
					return;
				}
				var arr = base64.split(',')
				var type = arr[0].match(/:(.*?);/)[1]
				var fileExt = type.split('/')[1]
				var bstr = atob(arr[1])
				var n = bstr.length
				var u8arr = new Uint8Array(n)
				while (n--) {
					u8arr[n] = bstr.charCodeAt(n)
				}
				return new File([u8arr], `${name}.` + fileExt, {
					type: type
				})
			},

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