Redactor 图片上传无法在 laravel 5 上运行
Redactor image upload not working on laravel 5
相同的代码在 laravel 4 中有效但在 laravel 5 中无效。
这是所有代码:
//Redactor Image Upload
Route::post('image/upload', function(){
$image = Input::file('file');
$filename = 'bdtimes'.rand(10, 99999999).'.'.$image->getClientOriginalExtension();
$move = Image::make($image->getRealPath())->save('uploads/images/original/'.$filename);
if($move){
return Response::json(['filelink'=>'/uploads/images/original/'. $filename]);
}else{
return Response::json(['error'=>true]);
}
});
编辑器脚本:
$(function()
{
$('#redactor').redactor({
focus: true,
imageUpload: '{{ url() }}/image/upload',
imageManagerJson: '{{ url() }}/image.php',
plugins: ['table', 'video','imagemanager','fontcolor','fontsize','fullscreen'],
maxHeight: 300,
minHeight: 300
});
});
在 Chrome 开发者工具中,当我尝试上传图片时显示此错误。
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:8000/image/upload
有什么问题?请帮助我。
谢谢
这意味着您的路线中的某些线路有错误。查看您的代码,我没有发现任何明显的问题,它可能与此处未显示的导入等有关。
尝试在每一行之后使用 dd() 打印调试信息,直到找到损坏的确切行。
您还可以查看来自 Chrome 开发人员工具的 ajax 请求响应,因为它必须包含有关确切错误的更多信息。
更新答案
令牌有问题。更改 Redactor 脚本..
$(function()
{
$('#redactor').redactor({
focus: true,
imageUpload: '{{ url() }}/image/upload?_token=' + '{{ csrf_token() }}',
imageManagerJson: '{{ url() }}/image.php',
plugins: ['table', 'video','imagemanager','fontcolor','fontsize','fullscreen'],
maxHeight: 300,
minHeight: 300
});
});
我在使用 yii 框架的 redactor 时遇到了同样的问题。我认为问题在于设置上传目录的路径。所以 redactor 的开发人员更改了一些代码来防止这种情况。
在 RedactorModule.php > public 函数 getSaveDir() 中更改为:
$path = Yii::getAlias($this->uploadDir);
if (!file_exists($path)) {
throw new InvalidConfigException('Invalid config $uploadDir');
}
if (FileHelper::createDirectory($path . DIRECTORY_SEPARATOR . $this->getOwnerPath(), 0777)) {
return $path . DIRECTORY_SEPARATOR . $this->getOwnerPath();
对此:
$path = Yii::getAlias($this->uploadDir) . DIRECTORY_SEPARATOR . $this->getOwnerPath();
if(!file_exists($path)){
if (!FileHelper::createDirectory($path, 0775,$recursive = true )) {
throw new InvalidConfigException('$uploadDir does not exist and default path creation failed');
}
}
return $path;
相同的代码在 laravel 4 中有效但在 laravel 5 中无效。
这是所有代码:
//Redactor Image Upload
Route::post('image/upload', function(){
$image = Input::file('file');
$filename = 'bdtimes'.rand(10, 99999999).'.'.$image->getClientOriginalExtension();
$move = Image::make($image->getRealPath())->save('uploads/images/original/'.$filename);
if($move){
return Response::json(['filelink'=>'/uploads/images/original/'. $filename]);
}else{
return Response::json(['error'=>true]);
}
});
编辑器脚本:
$(function()
{
$('#redactor').redactor({
focus: true,
imageUpload: '{{ url() }}/image/upload',
imageManagerJson: '{{ url() }}/image.php',
plugins: ['table', 'video','imagemanager','fontcolor','fontsize','fullscreen'],
maxHeight: 300,
minHeight: 300
});
});
在 Chrome 开发者工具中,当我尝试上传图片时显示此错误。
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:8000/image/upload
有什么问题?请帮助我。
谢谢
这意味着您的路线中的某些线路有错误。查看您的代码,我没有发现任何明显的问题,它可能与此处未显示的导入等有关。
尝试在每一行之后使用 dd() 打印调试信息,直到找到损坏的确切行。
您还可以查看来自 Chrome 开发人员工具的 ajax 请求响应,因为它必须包含有关确切错误的更多信息。
更新答案
令牌有问题。更改 Redactor 脚本..
$(function()
{
$('#redactor').redactor({
focus: true,
imageUpload: '{{ url() }}/image/upload?_token=' + '{{ csrf_token() }}',
imageManagerJson: '{{ url() }}/image.php',
plugins: ['table', 'video','imagemanager','fontcolor','fontsize','fullscreen'],
maxHeight: 300,
minHeight: 300
});
});
我在使用 yii 框架的 redactor 时遇到了同样的问题。我认为问题在于设置上传目录的路径。所以 redactor 的开发人员更改了一些代码来防止这种情况。 在 RedactorModule.php > public 函数 getSaveDir() 中更改为:
$path = Yii::getAlias($this->uploadDir);
if (!file_exists($path)) {
throw new InvalidConfigException('Invalid config $uploadDir');
}
if (FileHelper::createDirectory($path . DIRECTORY_SEPARATOR . $this->getOwnerPath(), 0777)) {
return $path . DIRECTORY_SEPARATOR . $this->getOwnerPath();
对此:
$path = Yii::getAlias($this->uploadDir) . DIRECTORY_SEPARATOR . $this->getOwnerPath();
if(!file_exists($path)){
if (!FileHelper::createDirectory($path, 0775,$recursive = true )) {
throw new InvalidConfigException('$uploadDir does not exist and default path creation failed');
}
}
return $path;