当前位置:   article > 正文

uniapp 压缩图片获取文件路径,通过路径获取file图片,进行MD5加密_uniapp 通过临时路径获取file文件

uniapp 通过临时路径获取file文件

uniapp 压缩图片获取文件路径,通过路径获取file图片,进行MD5加密

1、选择图片

plus.nativeUI.actionSheet({
	cancel: '取消',
	buttons: [{
		title: '拍照'
	}, {
		title: '相册'
	}]
  },
  (res) => {
	var index = e.index;
	switch (index) {
		case 0:
			// "取消";
			break;
		case 1:
			var cmr= plus.camera.getCamera();
			var res = cmr.supportedImageResolutions[0];
			var fmt = cmr.supportedImageFormats[0];
			cmr.captureImage((path) => {
					this.uploadPhoto(path);
				},
				(error) => {
					console.log("Capture image failed: " + error.message);
				}, {
					resolution: res,
					format: fmt
				}
			);
			break;
		case 2:
			plus.gallery.pick((path) => {
				this.uploadPhoto(path);
			}, (e) => {
				console.log("取消选择图片");
			}, {
				filter: "image"
			});
			break;
	}
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

2、压缩图片并进行MD5加密

//在项目中安装spark-md5
npm i spark-md5 --save


//在main.js文件中引入MD5
import SparkMD5 from 'spark-md5'
// vue原型链挂载
Vue.prototype.$sparkMD5 = SparkMD5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
function uploadPhoto(path){
	//let pathDst = '自定义路径'
    let pathDst = path.substring(0, path.lastIndexOf("."));
    //压缩图片
    plus.zip.compressImage(
      {
        src: path, //原路径
        dst: pathDst, //缩略图路径
        quality: 20, //1-100,1图片最小,100图片最大
		overwrite: true, // 覆盖文件 是否生成新图片
		format: 'jpg', // 格式
        width: "auto", //缩略固定宽
        height: "auto", //缩略固定高
      },
      () => {
    	//通过路径获取file
		path = pathDst;
		const _this = this
		plus.io.resolveLocalFileSystemURL(path, function(entry) { 
			entry.file(
				function(file){
				   var fileReader = new plus.io.FileReader();  
				   fileReader.readAsDataURL(file); 
				   fileReader.onloadend = function(e) {
    					//进行MD5加密
						let spark = new _this.$sparkMD5()
						spark.appendBinary(e.target.result)
						let md5 = spark.end()
						//上传文件
						_this.uploadFileFun(path, md5)
				   }
				}
			) 
			
		});
      }
    );
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

3、上传图片

function uploadFileFun(path, md5){
	//进行上传
	uni.uploadFile({
		url:  '上传地址' + `?md=${ md5 ? md5 : '' }`,
		filePath: path,//文件路径
		name: 'file',
		header: '上传header',
		success: (res) => {
			uni.showToast({title:"头像上传成功!", position: "bottom"})
		},
		fail: (res) => {
			uni.showToast({title:"头像上传失败!", position: "bottom"})
		}
	})
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

完整代码

function openPhoto() {
	plus.nativeUI.actionSheet({
		cancel: '取消',
		buttons: [{
			title: '拍照'
		}, {
			title: '相册'
		}]
	  },
	  (res) => {
		var index = e.index;
		switch (index) {
			case 0:
				// "取消";
				break;
			case 1:
				var cmr= plus.camera.getCamera();
				var res = cmr.supportedImageResolutions[0];
				var fmt = cmr.supportedImageFormats[0];
				cmr.captureImage((path) => {
						this.uploadPhoto(path);
					},
					(error) => {
						console.log("Capture image failed: " + error.message);
					}, {
						resolution: res,
						format: fmt
					}
				);
				break;
			case 2:
				plus.gallery.pick((path) => {
					this.uploadPhoto(path);
				}, (e) => {
					console.log("取消选择图片");
				}, {
					filter: "image"
				});
				break;
		}
	  },
	})
},
function uploadPhoto(path){
	//let pathDst = '自定义路径'
    let pathDst = path.substring(0, path.lastIndexOf("."));
    //压缩图片
    plus.zip.compressImage(
      {
        src: path, //原路径
        dst: pathDst, //缩略图路径
        quality: 20, //1-100,1图片最小,100图片最大
		overwrite: true, // 覆盖文件 是否生成新图片
		format: 'jpg', // 格式
        width: "auto", //缩略固定宽
        height: "auto", //缩略固定高
      },
      () => {
    	//通过路径获取file
		path = pathDst;
		const _this = this
		plus.io.resolveLocalFileSystemURL(path, function(entry) { 
			entry.file(
				function(file){
				   var fileReader = new plus.io.FileReader();  
				   fileReader.readAsDataURL(file); 
				   fileReader.onloadend = function(e) {
    					//进行MD5加密
						let spark = new _this.$sparkMD5()
						spark.appendBinary(e.target.result)
						let md5 = spark.end()
						//上传文件
						_this.uploadFileFun(path, md5)
				   }
				}
			) 
			
		});
      }
    );
},
function uploadFileFun(path, md5){
	//进行上传
	uni.uploadFile({
		url:  '上传地址' + `?md=${ md5 ? md5 : '' }`,
		filePath: path,//文件路径
		name: 'file',
		header: '上传header',
		success: (res) => {
			uni.showToast({title:"头像上传成功!", position: "bottom"})
		},
		fail: (res) => {
			uni.showToast({title:"头像上传失败!", position: "bottom"})
		}
	})
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/758934
推荐阅读