uniapp使用uviews上傳多張圖片

uniapp使用uviews上傳多張圖片,出現(xiàn)獲取的圖片路徑重復了,是因為放在了onchange中了,解決方法

    <u-upload :auto-upload="true" :action="action" :header="imgheader" :file-list="fileList"
                    @on-remove="delimg" max-count="9" @on-choose-complete="upimgchoose" :custom-btn="true" > 
          <view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
                <u-icon name="photo" size="60"></u-icon>
          </view>
   </u-upload>
upimgchoose(lists, name) {
         this.imglist = lists

         // this.imglist = this.imglist.filter(item => item.url.startsWith('http://tmp/'))
         //為了添加的時候二次添加圖片,獲取的路徑和第一次添加的路徑不同,過濾第二次的路徑
         this.imglist = this.imglist.filter(item => item.url.startsWith('wxfile://tmp'))
         this.fupimg(this.imglist)
 },
fupimg(lists) {
         console.log(lists, '選擇的')
            let that = this
         uni.request({
             url: that.configURL.BaseURL + 'XXX',
             header: {
                 'apply-secret': that.configURL.secret,
                 'Authorization': 'Bearer ' + uni.getStorageSync('dsshopApplytoken')
                 },
                 method: 'post',
                    async success(response) {
                        console.log(response)
                        let datas = response.data.message;
                        let uploadPromises = []
                        if (!that.edit) {
                        //添加的時候修改照片,將之前上傳的清空,否則會累積
                            that.form.imgs = []
                        }
                        for await (const item of lists) {
                            await uploadPromises.push(await that.uploadImage(datas.host, item.url, datas));
                        }
                        console.log(that.form, 'that.formthat.form')
                    },
                 fail(res) {
                  that.$api.msg(res.message);
              return false
         }
     })


 },
uploadImage(apiUrl, filePath, datas) {
         return new Promise((resolve, reject) => {
             let that = this
             const tempFilePath = filePath; // 獲取第一張選定的圖片的臨時路徑
             // 利用字符串操作函數(shù) split()、lastIndexOf() 等來提取圖片的后綴
             const lastDotIndex = tempFilePath.lastIndexOf('.'); // 查找最后一個點號的位置
             let fileExtension;
             if (lastDotIndex !== -1 && lastDotIndex < tempFilePath.length - 1) {
                 fileExtension = tempFilePath.substring(lastDotIndex); // 提取點號之后部分作為后綴 
             } else {
                    that.$api.msg('未能正確提取圖片后綴!');
                    return
             }
             const date = new Date().getFullYear()
             const date1 = new Date().getMonth() + 1
             const date2 = new Date().getDate()
             const date3 = (new Date()).valueOf()
             const a = datas.dir + '/' + 'weidms' + '/' + date + '/' + date1 +
                        '/' + date2 + '/' + date3 + fileExtension
                 console.log(filePath, 'filePath')
                 uni.uploadFile({
                     url: apiUrl,
                     name: "file",
                     filePath: filePath,
                     formData: {
                         key: a,
                         policy: datas.policy,
                         OSSAccessKeyId: datas.accessid,
                         signature: datas.signature,
                         // 'x-oss-security-token': securityToken // 使用STS簽名時必傳。
                  },
                success: (rs) => {
                     resolve(rs);
                     if (rs.statusCode === 204) {
                         that.form.imgs.push(datas.host + a)
                         }
                     },
                 fail: (error) => {
                     reject(error);
                 },
             });
         });
 },
    delimg(e) {
         console.log(e, '刪除的', this.imglist) 
         this.form.imgs.splice(e, 1) 
 },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容