微信小程序 laravel8 (多文件上传本地)

html页面

 <view class="uppic">
      <text class="iconfont icon-jiahao" bindtap="upfile"></text>
    </view>
    <view>
      <block wx:for="{{picture}}">
          <image src="{{item}}" style="height: 100rpx;width: 100rpx;"></image>
      </block>
    </view>

js逻辑

data: {
    picture:[],
    card:"",
    // pic:[]
  },

upfile(){
    let token = wx.getStorageSync('token');
    let openid = wx.getStorageSync('openid');
    let picture = [];
    let that = this;
    let card = "";
    wx.chooseMedia({
      count: 3,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success:res => {
       res.tempFiles.map(tempFilePath =>{
        wx.uploadFile({
          url: 'http://www.laravel8.com/api/uploadFile', //仅为示例,非真实的接口地址
          name: 'file',
          header:{'Authorization':'Bearer '+token},
          filePath: tempFilePath.tempFilePath,
          formData: {
            'openid':openid
          },
          success (res){
              let picture = res.data;
              let arr = that.data.picture;
              arr.push(picture);
              console.log(arr);
              if( arr.length>3 )
              {
                  return false;
              }
              that.setData({
                picture:arr
              })
             
          }
        })
       })
        
       
      }
    })
  },

php代码

下载compocer包

composer require itbdw/laravel-storage-qiniu 


 打开 config 文件夹下的 app.php 文件,在 providers 中加入一下代码

itbdw\QiniuStorage\QiniuFilesystemServiceProvider::class, 


 public function uploadFile(Request $request)
   {
       $file=$request->file('file')->store('','local');
       $pic = 'http://www.laravel8.com/app/'.$file;
       return $pic;
   }

config filesystems.php 添加配置


'local' => [
            'driver' => 'local',
            'root' => public_path('app'),
        ],


七牛云配置

'qiniu' => [
            'driver'    => 'qiniu',
            'domain'    => '',  //你的七牛域名
            'access_key'=> '',    //AccessKey
            'secret_key'=> '',   //SecretKey
            'bucket'    => '',    //Bucket名字,即七牛云存储空间名称
        ],
七牛云

    public function owners_picture(Request $request)
    {
        $token=$request->post('_token');
        $disk = \Storage::disk('qiniu'); //使用七牛云上传
        $time = date('Y-m-d');
        $filename = $disk->put($time, request()->file('image'));//上传
        if(!$filename) {
            return ['code'=>300,'mag'=>'失败','data'=>[]];
        }
        $img_url = $disk->getDriver()->downloadUrl($filename); //获取下载链接

        return ['code'=>200,'mag'=>'success','data'=>$img_url];
    }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值