Skip to content

Commit

Permalink
User can upload topic image
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Nov 1, 2017
1 parent 07b89be commit 4d0586a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions 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 @@ -63,4 +64,26 @@ public function destroy(Topic $topic)

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;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions resources/views/topics/create_and_edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
$(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
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
Route::resource('users', 'UsersController', ['only' => ['show', 'update', 'edit']]);
Route::resource('topics', 'TopicsController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);

Route::resource('categories', 'CategoriesController', ['only' => ['show']]);
Route::resource('categories', 'CategoriesController', ['only' => ['show']]);

Route::post('upload_image', 'TopicsController@uploadImage')->name('topics.upload_image');

0 comments on commit 4d0586a

Please sign in to comment.