• 微信小程序上传图片


     chooseImg:function() {
        let that = this
        wx.chooseImage({
          count: 1, // 默认9
          sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
          
          success: function (res) {
            console.log(res)
            var tempFilePaths = res.tempFilePaths
            that.data.urls = tempFilePaths[0] //图片地址
    
            that.setData({
              images: tempFilePaths[0],
              previewImage: that.data.urls
            })
    
            //启动上传等待中...
            wx.showToast({
              title: '正在上传...',
              icon: 'loading',
              mask: true,
              duration: 1000
            })
    
            //上传图片
            wx.uploadFile({
              url: app.domain +'teacherapi/Course_Table/uploadImg', 
              filePath: tempFilePaths[0],
              name: 'file',
              header: {  "Content-Type": "multipart/form-data" },
              success:function(res){
                console.log(res)
                console.log(res.data)
                var data = JSON.parse(res.data)   //转换成json
                console.log(data)
                if(data.status == 0){
                  // 上传成功
                  wx.showToast({
                    title: '上传成功',
                    success:function(){
                      that.setData({
                        chooesImg: data.msg
                      }) 
                    }
                    
                  })
                }else{
                  wx.showToast({
                    title: '上传失败',
                    image: '/images/icon/daodi.png',
                    duration: 1000
                  })
                  return;
                }
              },
              fail: function (res) {
                wx.hideToast();
                wx.showToast({
                  title: '上传失败',
                  image: '/images/icon/daodi.png',
                  duration: 1000
                })
                return;
              }
            })
          }
        })
      },

    wxml

    <view class='prew_img' wx:if="{{chooesImg}}">
      <image src='{{previewImage}}' bindtap="previewImage"/>
    </view>
  • 相关阅读:
    生活感悟(一)
    DOM数据制作(采用卫星遥感图像数据制作)
    对话框显示前的操作
    sqlHelper中DataReader的关闭问题
    整数的取余运算
    C#中的字符串格式String.Format
    SQL分页查询
    级联删除与更新的例子
    C#中的运算符重载(以重载+为例)
    [高效编程读书笔记]用readonly而不是const
  • 原文地址:https://www.cnblogs.com/zxf100/p/10025642.html
Copyright © 2020-2023  润新知