Skip to content

Commit

Permalink
用户可以上传话题图片
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent 538580d commit 7637220
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
25 changes: 24 additions & 1 deletion app/Http/Controllers/TopicsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Http\Requests\TopicRequest;
use App\Models\Category;
use Auth;
use App\Handlers\ImageUploadHandler;

class TopicsController extends Controller
{
Expand Down Expand Up @@ -62,5 +63,27 @@ public function destroy(Topic $topic)
$topic->delete();

return redirect()->route('topics.index')->with('message', 'Deleted successfully.');
}
}

public function uploadImage(Request $request, ImageUploadHandler $uploader)
{
// 初始化返回数据,默认是失败的
$data = [
'success' => false,
'msg' => '上传失败!',
'file_path' => ''
];
// 判断是否有上传文件,并赋值给 $file
if ($file = $request->upload_file) {
// 保存图片到本地
$result = $uploader->save($request->upload_file, 'topics', \Auth::id(), 1024);
// 图片保存成功的话
if ($result) {
$data['file_path'] = $result['path'];
$data['msg'] = "上传成功!";
$data['success'] = true;
}
}
return $data;
}
}
2 changes: 2 additions & 0 deletions public/uploads/images/topics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
10 changes: 10 additions & 0 deletions resources/views/topics/create_and_edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@
$(document).ready(function() {
var editor = new Simditor({
textarea: $('#editor'),
upload: {
url: '{{ route('topics.upload_image') }}',
params: {
_token: '{{ csrf_token() }}'
},
fileKey: 'upload_file',
connectionCount: 3,
leaveConfirm: '文件上传中,关闭此页面将取消上传。'
},
pasteImage: true,
});
});
</script>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
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');

0 comments on commit 7637220

Please sign in to comment.