/**
* 上传文件
* 前端点击事件,然后用chooseimage获取到本地图片的路径 然后用uploadfile将路径上传到后端
*/
file:function(){
wx.chooseImage({
count: 0,
success:function(res){
var file = (res.tempFilePaths)
wx.uploadFile({
filePath: file[0],
name: 'file',
url: 'http://www.yq.com/index.php/api/file',
})
}
})
}
后端
(1) 先下载命令
composer require itbdw/laravel-storage-qiniu
(2)在app/config/filesystems.php中加上
'qiniu' => [
'driver' => 'qiniu',
'domain' => '', //你的七牛域名
'access_key'=> '', //AccessKey
'secret_key'=> '', //SecretKey
'bucket' => '', //Bucket名字,即七牛云存储空间名称
],//ak sk需要自己在七牛云申请 domain我自己用的是腾讯云配置的加速域名,当然别的云服务器也可以
(3)在app/config/app.php['providers']中加上
itbdw\QiniuStorage\QiniuFilesystemServiceProvider::class,
(4)控制器方法中加上
/**
* 上传七牛云
*/
public function file(){
//接传过来的值
$file = \request()->file('file');
$disk = Storage::disk('qiniu');
//设置时间,防止重复上传导致的报错
$time = date('Y-m-d');
$filename = $disk->put($time,$file);
//这是上传七牛云后的返回地址
$url = $disk->getDriver()->downloadUrl($filename);
dd($url);
}
/**
将返回地址打印后就是这样的效果,将它返回到小程序端,存起来,将数据请求后端的时候一起加上入库就可以了
*/