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代码
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];
}
|