必须要先wx.config配置,jsApiList里面引入我们需要的微信Api
代码语言:javascript复制 wx.config({
debug: false,
appId: 'appId',
timestamp: 'timestamp',
nonceStr: 'nonceStr',
signature: 'signature',
jsApiList: ['chooseImage', 'getLocalImgData']
});
核心Js代码
代码语言:javascript复制 getupload() {
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
this.getpublish(res.localIds, 0)
}
});
},
getpublish(list, i) {
wx.getLocalImgData({
localId: list[i],
success: (res) => {
var localData = res.localData
//将base64转换为blob
let base = atob(localData.substring(localData.indexOf(',') 1));
let length = base.length;
let url = new Uint8Array(length);
while (length--) {
url[length] = base.charCodeAt(length);
}
let file = new File([url], 'a.jpg', {
type: 'image/jpg'
})
var formData = new FormData();
formData.append("file", file);
formData.append("key", "qqq");
axios({
method: "post",
url: "http://uploadFile",
data: formData
})
.then((res) => {
//采用递归上传
if (res.data.status == 200) {
this.file_list.push(res.data.info.url)
if (i 1 == list.length) {
console.log('上传成功')
}
if ( i < list.length) {
this.getpublish(list, i);
}
} else {
alert('上传失败')
}
})
}
});
},