上传图片

This commit is contained in:
fthvgb1 2018-01-01 18:48:03 +08:00
parent 37dc4afd5b
commit 1d2da78ae6
3 changed files with 36 additions and 2 deletions

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Http\Requests\TopicRequest;
use App\Models\Category;
use App\Models\Topic;
use App\Tools\ImageUploadTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@ -43,7 +44,30 @@ class TopicsController extends Controller
public function edit(Topic $topic)
{
$this->authorize('update', $topic);
return view('topics.create_and_edit', compact('topic'));
$categories = Category::all();
return view('topics.create_and_edit', compact('topic', 'categories'));
}
public function uploadImage(Request $request, ImageUploadTool $imageUploadTool)
{
// 初始化返回数据,默认是失败的
$data = [
'success' => false,
'msg' => '上传失败!',
'file_path' => ''
];
// 判断是否有上传文件,并赋值给 $file
if ($file = $request->upload_file) {
// 保存图片到本地
$result = $imageUploadTool->save($request->upload_file, 'topics', \Auth::id(), 1024);
// 图片保存成功的话
if ($result) {
$data['file_path'] = $result['path'];
$data['msg'] = "上传成功!";
$data['success'] = true;
}
}
return $data;
}
public function update(TopicRequest $request, Topic $topic)

View File

@ -11,7 +11,15 @@
<script>
$(document).ready(function () {
var editor = new Simditor({
textarea: $('#editor')
textarea: $('#editor'),
upload: {
url: '{{ route('topics.upload_image') }}',
params: {_token: '{{ csrf_token() }}'},
fileKey: 'upload_file',
connectionCount: 3,
leaveConfirm: '文件上传中,关闭此页面将取消上传。'
},
pasteImage: true
});
});
</script>

View File

@ -33,3 +33,5 @@ Route::post('password/reset', 'Auth\ResetPasswordController@reset');
Route::resource('topics', 'TopicsController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);
Route::resource('categories', 'CategoriesController', ['only' => ['show']]);
Route::post('upload_image', 'TopicsController@uploadImage')->name('topics.upload_image');