data:{
files:[],
previewfiles:[],
},
chooseImage:function (e) {
var self =this;
let files = self.data.files || [];
let maxLength = 3;
let count = maxLength - files.length;
wx.chooseImage({
count: count,
sizeType: ['original','compressed'],// 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album','camera'],// 可以指定来源是相册还是相机,默认二者都有
success:function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
let imageUrl = res.tempFilePaths;
console.log("chooseImage imageUrl=>", imageUrl)
var p = [];
imageUrl.forEach((it, index) => {
self.setData({
files: self.data.files.concat(it),
previewfiles: self.data.previewfiles.concat(it)
});
var myPromise =new Promise((resolve, reject) => {
self.uploadfileMultiple(it,function (relativePath) {
resolve(relativePath[0]);
});
});
p.push(myPromise);
});
console.log("p=>", p); // 数组
Promise.all(p).then(uploadFiles => {
console.log("relativePath uploadFiles=>", uploadFiles);
self.setData({
uploadFiles: uploadFiles
});
}, reason => {
console.log("relativePath reason=>", reason);
});
}
});
},
uploadfileMultiple:function (filePath, callback) {
var self =this;
var ticket = wx.getStorageSync('ticket');
try {
let formData = {
ticket: ticket,
folder:"folder/pic"
}
console.log("formData=>", formData)
wx.uploadFile({
url:'后端上传文件接口地址',// 仅为示例,非真实的接口地址
filePath: filePath,
name:'file',
formData: formData,
success(res) {
console.log("wx.uploadFile res=>", res);
let resData = res.data;
if (typeof resData =="string") {
resData = JSON.parse(resData);
}
if (resData.rstId == 1) {
let obj = resData.object || [];
let relativePath = obj[0].relativePath;
if (!!callback) {
callback([relativePath]);
}
}else {
wx.showToast({
title:'上传失败',
})
}
}
},true)
}catch (e) {
console.log(e)
}
},
网友回复
如何编写一个chrome插件实现多线程高速下载大文件?
cdn版本的vue在网页中出现typeerror错误无法找到错误代码位置怎么办?
pywebview能否使用webrtc远程控制共享桌面和摄像头?
pywebview6.0如何让窗体接受拖拽文件获取真实的文件路径?
如何在linux系统中同时能安装运行apk的安卓应用?
python有没有离线验证码识别ocr库?
各家的ai图生视频及文生视频的api价格谁最便宜?
openai、gemini、qwen3-vl、Doubao-Seed-1.6在ui截图视觉定位这款哪家更强更准?
如何在linux上创建一个沙箱隔离的目录让python使用?
pywebview如何使用浏览器自带语音识别与webspeech 的api?