• 技术文章 >web前端 >js教程

    微信小程序怎样使图片上传至服务器

    php中世界最好的语言php中世界最好的语言2018-04-14 16:42:25原创3214
    这次给大家带来微信小程序怎样使图片上传至服务器,微信小程序使图片上传至服务器的注意事项有哪些,下面就是实战案例,一起来看一下。

    -wxml
     <view class="shangchuan" bindtap="choose">
      <image style="width:100%;height:100%;" src="{{tempFilePaths}}"></image>
     </view>
     <button formType='submit' class="fabu">发布项目</button>
     /**选择图片 */
     choose: function () {
      var that = this
      wx.chooseImage({
       count: 1,
       sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
       sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
       success: function (res) {
        var tempFilePaths = res.tempFilePaths
        that.setData({
         tempFilePaths: res.tempFilePaths
        })
        console.log(res.tempFilePaths)
        wx.setStorage({ key: "card", data: tempFilePaths[0] })
       }
      })
     },

    2.使用wx.uploadFile将刚才上传的图片上传到服务器上

     formSubmit2: function (e) {
        var that = this
        var card = wx.getStorageSync('card')
        wx.uploadFile({
         url: app.globalData.create_funds,
         filePath: card,
         name: 'card',
         formData: {
          'user_id': app.globalData.user_id,
          'person': e.detail.value.person,
          'company': e.detail.value.company,
         },
         success: function (res) {
          console.log(res)
         }
        })
       }
      }
     },

    PS: 微信小程序上传一或多张图片

    一.要点

    1.选取图片

    wx.chooseImage({
       sizeType: [], // original 原图,compressed 压缩图,默认二者都有
       sourceType: [], // album 从相册选图,camera 使用相机,默认二者都有
       success: function (res) {
        console.log(res);
        var array = res.tempFilePaths, //图片的本地文件路径列表
       }
      })

    2.上传图片

    wx.uploadFile({
       url: '', //开发者服务器的 url
       filePath: '', // 要上传文件资源的路径 String类型!!!
       name: 'uploadFile', // 文件对应的 key ,(后台接口规定的关于图片的请求参数)
       header: {
        'content-type': 'multipart/form-data'
       }, // 设置请求的 header
       formData: { }, // HTTP 请求中其他额外的参数
       success: function (res) {
       },
       fail: function (res) {
       }
      })

    二.代码示例

    // 点击上传图片
    upShopLogo: function () {
      var that = this;
      wx.showActionSheet({
       itemList: ['从相册中选择', '拍照'],
       itemColor: "#f7982a",
       success: function (res) {
        if (!res.cancel) {
         if (res.tapIndex == 0) {
          that.chooseWxImageShop('album')  
         } else if (res.tapIndex == 1) {
          that.chooseWxImageShop('camera')
         }
        }
       }
      })
     },
     chooseWxImageShop: function (type) {
      var that = this;
      wx.chooseImage({
       sizeType: ['original', 'compressed'],
       sourceType: [type],
       success: function (res) {
    /*上传单张
        that.data.orderDetail.shopImage = res.tempFilePaths[0],
        that.upload_file(API_URL + 'shop/shopIcon', res.tempFilePaths[0])
    */
     /*上传多张(遍历数组,一次传一张)
        for (var index in res.tempFilePaths) {
           that.upload_file(API_URL + 'shop/shopImage', res.tempFilePaths[index])
        }
    */
       }
      })
     },
    upload_file: function (url, filePath) {
      var that = this;
      wx.uploadFile({
       url: url,
       filePath: filePath,
       name: 'uploadFile',
       header: {
        'content-type': 'multipart/form-data'
       }, // 设置请求的 header
       formData: { 'shopId': wx.getStorageSync('shopId') }, // HTTP 请求中其他额外的 form data
       success: function (res) {
        wx.showToast({
           title: "图片修改成功",
           icon: 'success',
           duration: 700
          })
       },
       fail: function (res) {
       }
      })
     },

    相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

    推荐阅读:

    在ES6里模板字符串使用详解

    在Bootstrap里怎么操作table

    以上就是微信小程序怎样使图片上传至服务器的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:服务器 小程序 上传
    上一篇:在ES6里模板字符串使用详解 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • react 怎么实现按需加载• react怎么实现滚动条• 一文聊聊node文件的读写操作• Angular学习之聊聊独立组件(Standalone Component)• 浅析Angular中的Change Detection机制
    1/1

    PHP中文网