解决uniapp中uni.chooseImage选择图片后临时路径转base64
由于 uni.chooseImage 不像 wx.chooseImage提供了直接转为base64的方法,有时候上传后台需要base64格式的图片,那么这时候就需要将临时路径转为base64格式。第一步:安装pathToBase64插件(https://ext.dcloud.net.cn/plugin?id=123)第二步:引入js并结合 uni.chooseImge 使用import { pat
·
由于 uni.chooseImage 不像 wx.chooseImage 提供了直接转为base64的方法,有时候上传后台需要base64格式的图片,那么这时候就需要将临时路径转为base64格式。
第一步:安装pathToBase64插件(https://ext.dcloud.net.cn/plugin?id=123)
第二步:引入js并结合 uni.chooseImge 使用
import { pathToBase64 } from '../js_sdk/mmmm-image-tools/index.js'
export default {
methods: {
choseImage() {
let _this = this;
uni.chooseImage({
count: 1, // 默认9
sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
success: function(res) {
//需要使用uni,getImageInfo获取图片的本地存储路径
uni.getImageInfo({
src: res.tempFilePaths[0],
success: (path) => {
pathToBase64(path.path).then(base64 => {
console.log(base64); // 这就是转为base64格式的图片
})
.catch(error => {
console.error(error)
})
}
})
}
});
},
}
}
最后通过base64传给后端,并在支持base64的Image标签上引用直接显示图片即可。
更多推荐
已为社区贡献5条内容
所有评论(0)