1. laravel 文件上传
public function store(Request $request) { // dd($request->file()); if ($request->hasFile('picture')) { $picture = $request->file('picture'); if (!$picture->isValid()) { abort(400, '无效的上传文件'); } // 文件扩展名 $extension = $picture->getClientOriginalExtension(); // 文件名 $fileName = $picture->getClientOriginalName(); // 生成新的统一格式的文件名 $newFileName = md5($fileName . time() . mt_rand(1, 10000)) . '.' . $extension; // 图片保存路径 $savePath = 'images/' . $newFileName; // Web 访问路径 $webPath = '/storage/'. $savePath; // 将文件保存到本地 storage/app/public/images 目录下,先判断同名文件是否已经存在,如果存在直接返回 if (Storage::disk('public')->has($savePath)) { return response()->json(['path' => $webPath]); } // 否则执行保存操作,保存成功将访问路径返回给调用方 if ($picture->storePubliclyAs('images', $newFileName, ['disk' => 'public'])) { return response()->json(['path' => $webPath]); } abort(500, '文件上传失败'); } else { abort(400, '请选择要上传的文件'); } }
<template> <div class="form-group"> <label for="picture">上传一张图片</label> <input type="file" class="form-control-file" id="picture" ref="picture" v-on:change="uploadFile"/> </div> </template> <script> export default { methods: { uploadFile() { let formData = new FormData(); formData.append('picture', this.$refs.picture.files[0]); axios.post( '/form/store', formData, { headers: { 'Content-Type': 'multipart/form-data' } } ).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); }); } } } </script>
2.