数据表单通过以下的调用来生成图片/文件上传表单,支持本地和云存储的文件上传,该文章介绍如何通过自定义上传路径处理文件上传和将文件存储到阿里云OSS。 1、上传组件是基于WEBuploader实现的,具体的使用配置可参考webuploade
数据表单通过以下的调用来生成图片/文件上传表单,支持本地和云存储的文件上传,该文章介绍如何通过自定义上传路径处理文件上传和将文件存储到阿里云OSS。
1、上传组件是基于WEBuploader实现的,具体的使用配置可参考webuploader官方文档。
2、通过自定义上传地址,实现定制化处理上传文件
3、使用juhedata/aliyun-oss组件完成oss图片上传
protected function fORMImg(Form $form)
{
$form->image('img', '图片')
->autoUpload() //选择图片后自动上传
->disableRemove() //禁止删除数据库存储的文件地址:防止误删
->disableAutoSave() //禁止上传成功后自动保存到数据库:根据项目需求
->accept('jpg,png,gif,jpeg', 'image
public function handle(Request $request, $type = 'banner')
{
//判断上传类型和文件上传规则是否存在
if (!isset($this->allowDir[$type]) || !isset($this->{$this->allowDir[$type]})) {
return $this->responseErrorMessage(errorMsg(1001));
}
$file = $this->file();
// 上传的文件属性;如 article:文章,校验在 OssUpload 类
if (!is_file($file)) {
return $this->responseErrorMessage('上传文件不存在或错误');
}
// 判断文件是否上传成功
if (method_exists($file, 'isValid') && $file->isValid()) {
// 校验上传的文件
if (($url = $this->uploadFile($file, $type) && is_numeric($url)) {
return $this->responseErrorMessage($url.errorMsg($url, '文件上传异常'));
}
return $this->responseUploaded($url, $url);
}
return $this->responseErrorMessage('文件上传失败');
}
}
安装上传组件
composer require juhedata/aliyun-oss
添加OSS文件
将OSS文件复制到App/Services目录下或自定义的位置
protected function uploadFile($file, $dir = 'banner')
{
$bucket = 'sdkcn';
$filePath = $file->path();
$extension = $file->guessClientExtension();
$imgName = time() . '-' . mb_substr(md5("sdk-img:" . $rule), 8, 16);
$pathName = $dir . '/img/' . date('Ym') . '/' . $imgName . '.' . $extension;
try {
OSS::publicUpload($bucket, $pathName, $filePath, ['ContentType' => $file->getMimeType()]);
return OSS::getPublicObjectURL($bucket, $pathName) . $suffix;
} catch (Exception $e) {
return 1006;
}
}
--结束END--
本文标题: Dcat Admin 入门应用(五)文件上传之OSS自定义上传
本文链接: https://www.lsjlt.com/news/99312.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0