【发布时间】:2021-04-25 09:48:21
【问题描述】:
我正在使用 Laravel 8 和 JavaScript 开发一个网站。 我使用ckeditor 5,它工作正常。当我尝试上传照片时出现问题。照片上传完成,但我收到一条错误消息并且没有显示照片。我收到以下标题的错误: “无法上传文件:filename.jpg” 我想我没有向前端返回正确的响应。 我在 route/web.php 文件中有这些代码
Route::group(["middleware"=>"auth","prefix"=>"panel"],function (){
Route::post('/post/edit/{post_slug}', [PostController::class,"update"])->name("updatePostRoute");
Route::get('/post/edit/{post_slug}', [RenderPanelController::class,"renderPostEditPage"])->name("renderPostEditPageRoute");
Route::post('/post/ckeditor/upload', [PostController::class,"upload_image_cke"])->name('ckeditor.upload');
});
在 PostController 中
public function upload_image_cke(Request $request){
if ($request->hasFile('upload')) {
$originName = $request->file('upload')->getClientOriginalName();
$fileName = pathinfo($originName, PATHINFO_FILENAME);
$extension = $request->file('upload')->getClientOriginalExtension();
$fileName = $fileName . '_' . time() . '.' . $extension;
$request->file('upload')->move(public_path('media'), $fileName);
$CKEditorFuncNum = $request->input('CKEditorFuncNum');
$url = asset('media/' . $fileName);
$msg = 'upload successfully';
$response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
@header('Content-type: text/html; charset=utf-8');
echo $response;
}
}
在刀片文件中:
<textarea type="text" class="form-control" name="content" id="content"></textarea>
<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="https://cdn.ckeditor.com/ckeditor5/27.1.0/classic/ckeditor.js"></script>
<script>
ClassicEditor
.create( document.querySelector( '#content' ), {
ckfinder: {
uploadUrl: '{{route('ckeditor.upload')."?command=QuickUpload&type=Files&responseType=json"}}',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
}
}
},{
alignment: {
options: [ 'right', 'right' ]
}} )
.then( editor => {
console.log( editor );
})
.catch( error => {
console.error( error );
})
</script>
我需要返回什么响应才能使照片上传正常工作?
【问题讨论】: