天天看點

uni-app 異步上傳圖檔uni-app 異步上傳圖檔

uni-app 異步上傳圖檔

因為執行順序的原因導緻部分手機選中圖檔無法上傳,或者上傳完成拿不到傳回值

async upload(){

const [err,{tempFilePaths}] = await uni.chooseImage({count: 9});

this.imgList = this.imgList.concat(tempFilePaths);

this.uploadTem(tempFilePaths);

},

async uploadTem(tempFilePaths) {

if(!tempFilePaths.length) {

return

}

const item = tempFilePaths.pop();



const res= await uni.uploadFile({

url: 'https://h5.grasslandfarming.com/api/imgupload', //僅為示例,非真實的接口位址

filePath: item,

name: 'file',



});

const [err,{data}] = res;

this.uploadImg.push(JSON.parse(data).result);

this.uploadTem(tempFilePaths);

},

//預覽

previewImage(index){

uni.previewImage({

urls: this.imgList,

current:index,

});

},