zoukankan      html  css  js  c++  java
  • 小程序上传图片到七牛

    小程序上传图片非常简单,利用好2个API:

    wx.chooseImage

    wx.uploadFile

    /**
       * 上传图片
       */
      chooseImageUpload() {
        var that = this;
        wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: function (res) {
            that.tempFilePaths = res.tempFilePaths;
            that.uploadQiniu();
          }
        })
      },
    

    注意点:

    • header头要写
    • token 一定要去获取七牛的token
    • filePath是单个图片就写下标0,如果多图片就用for循环吧,不过上个函数wx.chooseImage.count要配置1以上
    • url写https://upload-z2.qiniup.com
    /**
       * 上传七牛返回key
       */
      uploadQiniu() {
        let token = this.uploadtoken.token;
        let tempFilePaths = this.tempFilePaths;
        var that = this;
        wx.uploadFile({
          url: 'https://upload-z2.qiniup.com',
          name: 'file',
          filePath: tempFilePaths[0],
          header: {
            "Content-Type": "multipart/form-data"
          },
          formData: {
            token: token,
          },
          success: function (res) {
            let data = JSON.parse(res.data)
            // to do ...
          },
          fail: function (res) {
            console.log(res)
          }
        })
      },
    

    su

    看到返回key就是成功啦!!

  • 相关阅读:
    Java中的==和equals区别
    2014年06月30日
    20140625 20:39
    20140627 20:47
    2014年06月30日
    20140628 16:07
    hdu 1418 抱歉 (数学)
    hdu 1302 The Snail (模拟)
    hdu 1391Number Steps
    hdu 1395 2^x mod n = 1
  • 原文地址:https://www.cnblogs.com/liangfengbo/p/9117718.html
Copyright © 2011-2022 走看看