微信小程序----封装上传图片以及获取图片【wx.chooseMedia wx.uploadFile】

微信小程序----封装上传图片以及获取图片【wx.chooseMedia wx.uploadFile】

封装的js

// 选择图片
function chooseMedia() {
  return new Promise((resolve, reject)=>{
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success: (res) => {
        let tempFilePath = res.tempFiles[0].tempFilePath;
        resolve(tempFilePath)
      },
      fail:(res)=>{
        reject(res)
      }
    })
  })
  
}

// 上传图片到服务器
function uploadFile() {
  return new Promise((resolve, reject)=>{
    wx.showLoading();
    chooseMedia().then(path=>{
      wx.uploadFile({  
        filePath: path,
        name: 'avatar',
        url: getApp().globalData.baseURL + 'login/uploadAvatar',
        formData: {
          'data': 'bank_img'
        },
        success: (res) => {
          wx.hideLoading();
          const datas = JSON.parse(res.data); //把数据字符串转变成对象
          resolve(datas)
        },
        fail:(res)=>{
          reject(res)
        }
      })
    })
  })
}
module.exports = {
  uploadFile
}

调用封装的函数

<view class="imgt" bindtap="upimgBtn">
      <image class="imgz boRadius50" src="{{avatarUrl ||'/images/arv.png'}}" mode="aspectFill"></image>
</view>
const proFunc = require('../../utils/profunc');
//上传图片到服务器
  upimgBtn() {
    let t = this;
    proFunc.uploadFile().then(res => {
      let avatarUrl= res.data.complete_img;
      t.setData({
            avatarUrl,
          })
      })
  },
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过请求网页获取信息并显示,你可以使用微信小程序提供的网络请求 API wx.request()。具体步骤如下: 1. 在小程序的 app.json 文件中配置要请求的网页域名,如: ``` "networkTimeout": { "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }, "debug": true, "networkTimeout": { "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }, "appid": "wx1234567890", "pages": [ "pages/index/index" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "小程序", "navigationBarTextStyle": "black" }, "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/about/about", "text": "关于" } ] }, "networkTimeout": { "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }, "permission": { "scope.userLocation": { "desc": "小程序需要获取你的地理位置" } }, "window": { "navigationBarBackgroundColor": "#ffffff", "navigationBarTextStyle": "black", "navigationBarTitleText": "小程序", "backgroundColor": "#eeeeee", "backgroundTextStyle": "light" }, "sitemapLocation": "sitemap.json", "requiredBackgroundModes": ["audio"] ``` 2. 在小程序的 js 文件中使用 wx.request() 方法请求网页,并获取数据,如: ``` wx.request({ url: 'https://www.example.com/data.json', success: function(res) { console.log(res.data) } }) ``` 3. 将获取到的数据显示在小程序的界面上,如: ``` Page({ data: { info: '' }, onLoad: function() { var that = this wx.request({ url: 'https://www.example.com/data.json', success: function(res) { that.setData({ info: res.data }) } }) } }) ``` 其中,info 是一个数据绑定变量,用来保存从网页获取的数据。在 onLoad 方法中,使用 that.setData() 方法将获取到的数据更新到界面上。在小程序的 wxml 文件中,可以使用 {{info}} 引用该数据绑定变量,将数据显示在界面上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值