收藏
回答

据我在网上查询到的结果,上传图片到自己的服务器就没人成功过,这是什么原因。

uploadImage: function () {

    var that = this

    wx.chooseImage({

      success: function (res) {

        var tempFilePaths = res.tempFilePaths

        wx.showToast({

          title: '识别中,请稍候',

          icon: 'loading',

          duration: 2000

        })

        console.log(tempFilePaths[0])

        wx.uploadFile({

          url: 'https://www.hanqi86.wang/uploads/WX/',

          header: {

            'content-type': 'multipart/form-data'

          },

          filePath: tempFilePaths[0],

          name: 'file',

          formData: {

            'user': 'test'

          },

          success: function (res) {

            console.log("上传成功");

            console.log(res.data)

            wx.hideToast()

            var data = JSON.parse(res.data)

            if (!data.attributes) {

              that.setData({

                userInfo: {

                  avatarUrl: data.url,

                  tips: '未识别到颗粒'

                }

              })

              return

            }

            const genders = {

              'Male': '帅哥',

              'Female': '美女'

            }

            that.setData({

              userInfo: {

                avatarUrl: data.url,

                tips: '一位' + data.attributes.age.value + '岁的' + genders[data.attributes.gender.value]

              }

            })

            //do something

          },

          fail: function (res) {

            console.log("上传失败" + res.data);

            wx.showToast({

              title: '上传失败',

              icon: 'loading',

              duration: 2000

            })

          }

        })


        console.log("完成");

        wx.showToast({

          title: '识别完成',

          icon: 'loading',

          duration: 2000

        })

      }

    })

  }


回答关注问题邀请回答
收藏

15 个回答

  • Panda
    Panda
    2017-06-07

    我的还是没有解决掉,谁有好的例子,能否一看

    2017-06-07
    有用
    回复
  • 麦琪
    麦琪
    2017-04-13

    个人实操经验,主要还是后端的问题,前端只要按照例子copy然后加点自己的data数据就行了。

    2017-04-13
    有用
    回复
  • 回梦無痕
    回梦無痕
    2017-04-11

    我的成功案例

    upload_face:function()

      {

        var _this=this;

        wx.chooseImage({

          success: function(res) {

            wx.showToast({

              title: '加载中',

              icon: 'loading',

              duration: 10000

            })

            var tempFilePaths = res.tempFilePaths;

            wx.uploadFile({

              url: 'https://upload.****.com/miniapp/image', 

              filePath: tempFilePaths[0],

              name: 'file',

              formData:{

              uid: lib.getuid()

              },

              success: function(res)

              {

                //do something

                wx.showModal({

                          title: '提示',

                          content: '上传成功'

                })

              },

              fail:function()

              {

                wx.showModal({

                          title: '提示',

                          content: '上传失败'

                })

              }

            })

          }

        })

      },


    2017-04-11
    有用
    回复
  • 追梦的人(IT大叔一枚)
    追梦的人(IT大叔一枚)
    2017-04-11

    您看您的小程序中的服务器域名有没有设置上传对应的域名,很多时候会忽略这一点.

    单图片很容易,多图片就麻烦些了.

    2017-04-11
    有用
    回复
  • song
    song
    2017-04-11

    首先wx.chooseImage获取tempFilePaths 之后调用 wx.uploadFile就OK  功能是好用的

    2017-04-11
    有用
    回复

正在加载...

登录 后发表内容