之前在写EmlogApp的时候有用到七牛云上传图片,简单记录一下
insertImage() {
const that = this;
uni.chooseImage({
count: 1,
success: (res) => {
uni.showLoading({
title: '正在上传...'
});
uni.uploadFile({
url: '域名',
filePath: res.tempFilePaths[0],
name: 'file',
method:'POST',
fileType:"image",
formData: {
ak: ,//七牛云ak
sk: ,//七牛云sk
kj: ,//七牛云空间名
//ym: ,七牛绑定的域名,这个直接在PHP代码中修改
},
success: function(res) {
var data = JSON.parse(res.data)
console.log(data.data)
that.editorCtx.insertImage({
src: data.data,
alt: data.data,
success: function() {
that.cover.push(data.data)
that.messageToggle('success', '上传成功');
uni.hideLoading();
}
})
},
fail: function() {
that.messageToggle('error', '上传失败');
uni.hideLoading();
}
})
}
})
},
// 七牛云上传
public function img(Request $request){
$file = request() -> file('file'); //接收前端数据
$filePath = $file-> getRealPath();//要上传图片的本地路径
$ext = (md5(time()));//上传到七牛后保存的文件名
$accessKey = $_POST['ak'];
$secretKey = $_POST['sk'];
$auth = new Auth($accessKey, $secretKey);//构建鉴权对象
// $bucket = "hkiii";//要上传的空间
$bucket = $_POST['kj'];
//初始化UploadManager对象并进行文件的上传
$uploadMgr = new UploadManager();
$token = $auth->uploadToken($bucket); //生成上传Token
list($ret, $error) = $uploadMgr->putFile($token, $ext, $filePath);
if ($ret) {
return json(['code' => 200, 'msg' => '上传成功', 'data' => 'https://cdn.hkiii.cn/'.$ret['key']]);
}
}