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)
}
},
网友回复
为啥所有的照片分辨率提升工具都会修改照片上的图案细节?
js如何在浏览器中将webm视频的声音分离为单独音频?
微信小程序如何播放第三方域名url的mp4视频?
ai多模态大模型能实时识别视频中的手语为文字吗?
如何远程调试别人的chrome浏览器获取调试信息?
为啥js打开新网页window.open设置窗口宽高无效?
浏览器中js的navigator.mediaDevices.getDisplayMedia屏幕录像无法录制SpeechSynthesisUtterance产生的说话声音?
js中mediaRecorder如何录制window.speechSynthesis声音音频并下载?
python如何直接获取抖音短视频的音频文件url?
js在浏览器中如何使用MediaStream与MediaRecorder实现声音音频多轨道混流?