data: { imgurl:'' }, chooseImage1(){ var that = this; wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success(res) { const tempFilePaths = res.tempFilePaths[0] wx.uploadFile({ url: 'https://。。。。。。。/imageUpload', //開發者服務器的 url filePath: tempFilePaths, // 要上傳文件資源的路徑 String類型!!! name: 'img', // 文件對應的 key ,(后台接口規定的關於圖片的請求參數) header: { 'content-type': 'multipart/form-data', 'token': wx.getStorageSync('token') }, // 設置請求的 header formData: {}, // HTTP 請求中其他額外的參數 success: function (res) { console.log(res.data); var ob = JSON.parse(res.data); console.log(ob) that.setData({ imgurl: 'https://。。。。。/'+ob.data }) }, fail: function (res) { } }) } }) }
上傳圖片代碼就是上面的這些,但是會出現有時候能上傳上去有時候不能,原來是小程序做了限制,大概在2m以內
大家覺得不錯的話可以支持一下