赞
踩
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;
}
},
});
//在项目中安装spark-md5
npm i spark-md5 --save
//在main.js文件中引入MD5
import SparkMD5 from 'spark-md5'
// vue原型链挂载
Vue.prototype.$sparkMD5 = SparkMD5
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"})
}
})
}
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"})
}
})
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。