Laravel 在 CKEDITOR 上获取上传的图片
Laravel get uploaded image on CKEDITOR
当我使用 ckeditor
上传图片并将其附加到 post 时,我在控制器中的上传图片功能工作正常,没有任何问题,但是当我想 return 上传图片时对此,ckeditor 无法理解,例如这是我的代码:
控制器:
public function uploadImageContent()
{
$this->validate(request(), [
'upload' => 'mimes:jpeg,jpg,gif,png'
]);
$file = request()->file('upload');
$filename = $file->getClientOriginalName();
$year = Carbon::now()->year;
$imagePath = "/uploads/post_images/{$year}/";
if (file_exists(public_path($imagePath) . $filename)) {
$filename = Carbon::now()->timestamp . '.' . $filename;
}
$file->move(public_path() . $imagePath, $filename);
$url = $imagePath . $filename;
return "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
}
这个函数工作正常,我在 console
或 network
上没有收到任何错误
return "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
应该是 return 路径,但不起作用。
查看:
<script>
$(function () {
CKEDITOR.replace('description', {
height: '200px',
extraPlugins: 'forms',
filebrowserUploadUrl:'/dashboard/administrator/attachImage',
filebrowserImageUploadUrl:'/dashboard/administrator/attachImage'
});
});
</script>
路线:
Route::group(['namespace' => 'Dashboard', 'prefix' => 'dashboard'], function () {
$this->group(['prefix' => 'administrator'], function () {
...
$this->post('/attachImage', 'ContentsController@attachImage');
...
});
内容控制器:
class ContentsController extends Controller
{
...
public function attachImage()
{
$this->uploadImageContent(request()->all());
}
}
使用 echo
而不是 return
解决了我的问题:
echo "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
我在 laravel 5.5
上有这个问题
你的代码对我不起作用。我观察到的是,您没有在 1
的位置嵌入 CKEditorFuncNum
(服务器接收为 POST 变量)作为 callFunction()
的第一个参数。我用 $request->CKEditorFuncNum
替换了 1
然后我使用 return
语句而不是 echo
并且一切正常。
这是您的代码:
echo "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
和
这是我的代码:
return "<script>window.parent.CKEDITOR.tools.callFunction('{$request->CKEditorFuncNum}','{$url}','')</script>";
我在Laravel 5.8
希望对其他人有所帮助。
当我使用 ckeditor
上传图片并将其附加到 post 时,我在控制器中的上传图片功能工作正常,没有任何问题,但是当我想 return 上传图片时对此,ckeditor 无法理解,例如这是我的代码:
控制器:
public function uploadImageContent()
{
$this->validate(request(), [
'upload' => 'mimes:jpeg,jpg,gif,png'
]);
$file = request()->file('upload');
$filename = $file->getClientOriginalName();
$year = Carbon::now()->year;
$imagePath = "/uploads/post_images/{$year}/";
if (file_exists(public_path($imagePath) . $filename)) {
$filename = Carbon::now()->timestamp . '.' . $filename;
}
$file->move(public_path() . $imagePath, $filename);
$url = $imagePath . $filename;
return "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
}
这个函数工作正常,我在 console
或 network
return "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
应该是 return 路径,但不起作用。
查看:
<script>
$(function () {
CKEDITOR.replace('description', {
height: '200px',
extraPlugins: 'forms',
filebrowserUploadUrl:'/dashboard/administrator/attachImage',
filebrowserImageUploadUrl:'/dashboard/administrator/attachImage'
});
});
</script>
路线:
Route::group(['namespace' => 'Dashboard', 'prefix' => 'dashboard'], function () {
$this->group(['prefix' => 'administrator'], function () {
...
$this->post('/attachImage', 'ContentsController@attachImage');
...
});
内容控制器:
class ContentsController extends Controller
{
...
public function attachImage()
{
$this->uploadImageContent(request()->all());
}
}
使用 echo
而不是 return
解决了我的问题:
echo "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
我在 laravel 5.5
你的代码对我不起作用。我观察到的是,您没有在 1
的位置嵌入 CKEditorFuncNum
(服务器接收为 POST 变量)作为 callFunction()
的第一个参数。我用 $request->CKEditorFuncNum
替换了 1
然后我使用 return
语句而不是 echo
并且一切正常。
这是您的代码:
echo "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>";
和
这是我的代码:
return "<script>window.parent.CKEDITOR.tools.callFunction('{$request->CKEditorFuncNum}','{$url}','')</script>";
我在Laravel 5.8
希望对其他人有所帮助。