微信小程序上傳單張或多張圖片


--

chooseImage: function () {
        let that = this;
        let worksImgs = that.data.worksImgs;
        let len = that.data.worksImgs.length;
        wx.chooseImage({
            count: 9 - len, //最多選擇9張圖片
            sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
            sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
            success: function (res) {
                console.log(res);
                // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片
                if (res.tempFilePaths.count == 0) {
                    return;
                }
                let tempFilePaths = res.tempFilePaths;
                // let token = app.data.uptoken;
                //上傳圖片 循環提交
                for (let i = 0; i < tempFilePaths.length; i++) {
                    wx.uploadFile({
                        url: 'url', //此處換上你的接口地址 
                        filePath: tempFilePaths[i],
                        name: 'upload_file',
                        header: {
                            "Content-Type": "multipart/form-data",
                            'accept': 'application/json',
                            
                        },
                        success: function (res) {
                            console.log(res);
                            let data = JSON.parse(res.data); // 這個很關鍵
                            worksImgs.push(data.data.url);
                            that.setData({
                                worksImgs: worksImgs
                            })
                        }
                    })
                }
 
            }
        })
    },
    // 刪除上傳的圖片
    deleteImg: function (e) {
        var worksImgs = this.data.worksImgs;
        var itemIndex = e.currentTarget.dataset.index;
        worksImgs.splice(itemIndex, 1);
        this.setData({
            worksImgs: worksImgs
        })
    },
    // 提交個人作品
    submitWorks:function(){
        let that = this;
        let worksImgs = String(that.data.worksImgs);
        let obj = {
            store_id: that.data.store_id,
            mode_id: that.data.mode_id,
            works_img: worksImgs,
            works_info: that.data.works_info, 
            is_xs : 1
        }
        if (obj.works_img.length == 0 || obj.works_info == ''){
            wx.showModal({
                title: '提示',
                content: '數據不能為空!',
                showCancel: false,
            })
        }else{
            utils.postRequest('Mode/worksAdd', obj, '加載中', (res) => {
                if (res.data.rc == 200) {
                    wx.showModal({
                        title: '提示',
                        content: '作品上傳成功',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                that.setData({
                                    isUpWork: false
                                })
                                that.onShow();
                            }
                        }
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '作品上傳失敗',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                that.setData({
                                    isUpWork: false
                                })
                                that.onShow();
                            }
                        }
                    })
                }
            })
        }
        
    },

 

--


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM