国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序-拍照或選擇圖片并上傳文件

2019-11-19 18:05:34
字體:
來源:轉載
供稿:網友

微信小程序-拍照或選擇圖片并上傳文件

調用拍照API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html?t=20161222#wxchooseimageobject

上傳文件API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-file.html

主要js代碼:

choice: function () {   var that = this   wx.chooseImage({    count: 1, // 默認9    sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有    sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有    success: function (res) {     // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片     var tempFilePaths = res.tempFilePaths     that.setData({      textHidden: true,      image_photo: tempFilePaths,      photoHidden: false     })    }   })  },  uploadPhoto: function () {   var that = this   let param = util.json2Form({    tel: '18600346580',    orderSn: that.data.orderSn,    parkingPhoto: that.data.image_photo,   });   wx.uploadFile({    url: 'https://testapi.****.com/v4.0.0/uploadParkingPhoto', //僅為示例    filePath: that.data.image_photo[0],    name: 'parkingPhoto',    formData: {     'tel': '***********',     'orderSn': that.data.orderSn,    },    success: function (res) {     var obj = JSON.parse(res.data);;     console.log(obj.result)     console.log(obj.msg)     var resule = obj.result;     var msg = obj.msg;     if (resule == 'false') {      wx.showToast({       title: msg,       icon: 'success',       duration: 2000      })     } else {      wx.navigateBack({       delta: 1,       success: function (res) {        wx.showToast({         title: msg,         icon: 'success',         duration: 2000        })       },       })     }    }   })   } 

Tip:

1.調用wx.chooseImage() 自動彈出選擇窗口,不用調用操作菜單wx.showActionSheet(),否則重復

如圖

2. 上傳文件的時候filePath必須是數組,當你單張的時候需要使用數組,若只有一張要用[0]

3. 最需要注意的是,success返回數據是String類型,我們需要將String類型轉換成Object

js字符串轉換成obj

用js 是有三種方法的

  1. js自帶的eval函數,其中需要添加小括號eval('('+str+')');
  2. JSON.parse(str)
  3. $.parseJSON( jsonstr )

但是在微信小程序中,我們只能用JSON.parse(str),其他的都不可以

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 思南县| 茂名市| 耿马| 巴东县| 抚顺市| 黄大仙区| 岚皋县| 苍溪县| 库尔勒市| 南通市| 安新县| 肇州县| 开封市| 开封县| 淮北市| 东阳市| 手游| 察雅县| 聂荣县| 襄城县| 南汇区| 香港 | 日喀则市| 利津县| 仁怀市| 宜川县| 霍邱县| 墨玉县| 沅陵县| 基隆市| 靖州| 额济纳旗| 万安县| 惠州市| 张家界市| 宣恩县| 扎兰屯市| 高雄市| 永春县| 临夏县| 安康市|