收藏
回答

获取用户头像临时路径后如何上传到自己的服务器?

wx.uploadFile({

                            url:

                            filePath: this.data.avatarUrl, // 临时路径

                            name: 'file', // 服务器接收的字段名

                            data: wx.getStorageSync('openid'),

                            success(uploadRes) {

                              console.log('图片上传成功',uploadRes);


  onChooseAvatar(e) {

    const { avatarUrl } = e.detail 

    this.setData({

      avatarUrl,

    })

  },

filePath: this.data.avatarUrl, // 临时路径这样写对吗?为什么服务器返回上传失败

服务器源码:

    function image(){

        $input = input();

        $id = isset($input['id']) ? $input['id'] : '';

        $uploadDir = Config::get('filesystem.disks.public.root') . 'userimage';


        $file = Request::instance()->file('image');

        $info = $file->move($uploadDir);

        if ($info) {

            $imageUrl = Config::get('view_replace_str.__UPLOAD__') . $info->getSaveName();

            

            return $this->success('图片上传成功', ['url' => $imageUrl,$id]);

        } else {

            return $this->error('图片上传失败:' . $file->getError());

        }

    }

    

正常请求结果如下{

    "code": 1,

    "msg": "图片上传成功",

    "time": "1715419323",

    "data": {

        "0": "456",

        "url": "20240511/eeacd39a338200be41bc38b2d3c51ea3.png"

    }

}


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

2 个回答

  • Mr.Zhao
    Mr.Zhao
    发表于小程序端
    05-11

    不会php能看出来点问题,小程序是name:file,那么php获取图片就是file('file')

    05-11
    有用
    回复
  • 拾忆
    拾忆
    05-11

    获取头像返回的是http公网访问的路径,你需要通过wx.downloadFile下载到本地,获取本地的临时路径再上传。

    05-11
    有用
    回复 3
    • 绍苏777
      绍苏777
      05-11
      http://tmp/6N0apKAOebJ33fb12542a454478438038505f800c7f7.jpeg使用这个下载吗?
      05-11
      回复
    • 绍苏777
      绍苏777
      05-11
      服务器提示空值$info = $file->move($uploadDir);↵Call to a member function move() on null
      05-11
      回复
    • 拾忆
      拾忆
      05-11回复绍苏777
      哦,记错了,那这个就是临时路径了,正常上传就可以了。
      但是手机端和PC端路径是不一样的,建议用手机端试。
      05-11
      回复
登录 后发表内容