我正在使用Blueimp.jquery file-upload进行文件上传。我的应用也在Laravel 4
框架下,我在上传文件时出错。
Firefox和Chrome中此API
返回的错误是:
Error: Method Not Allowed
我认为错误来自routing
Laravel
,因为这是我在Firebug
开发控制台上找到的内容:
{"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException","message":"","file":"C:\\xampp\\htdocs\\digisells\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php","line":210}}
这是表格代码:
{{ Form::open(['route'=>'image.store','id'=>'fileupload','files'=>true]) }}
<div class="col-md-12">
<div class="container">
<!-- file upload -->
<!-- <form id="fileupload" action="file-upload/product-images" method="POST" enctype="multipart/form-data"> -->
<!-- Redirect browsers with JavaScript disabled to the origin page -->
<noscript><input type="hidden" name="redirect" value="http://blueimp.github.io/jQuery-File-Upload/"></noscript>
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="row fileupload-buttonbar">
<div class="col-md-7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Add files...</span>
<input type="file" name="thumbnail" multiple>
</span>
<button type="submit" class="btn btn-primary start">
<i class="glyphicon glyphicon-upload"></i>
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="glyphicon glyphicon-ban-circle"></i>
<span>Cancel upload</span>
</button>
<button type="button" class="btn btn-danger delete">
<i class="glyphicon glyphicon-trash"></i>
<span>Delete</span>
</button>
<input type="checkbox" class="toggle">
<!-- The global file processing state -->
<span class="fileupload-process"></span>
</div>
<!-- The global progress state -->
<div class="col-md-5 fileupload-progress fade">
<!-- The global progress bar -->
<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="progress-bar progress-bar-success" style="width:0%;"></div>
</div>
<!-- The extended global progress state -->
<div class="progress-extended"> </div>
</div>
</div>
<!-- The table listing the files available for upload/download -->
<table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
<!-- </form> -->
</div>
</div>
{{ Form::close() }}
这是表单的routes
代码:
Route::resource('image', 'ImageUploadController');
在ImageUploadController
里面我有:
public function store()
{
$file = Input::file('thumbnail');
$file = move(public_path().'/product/images/temp/', $file=>getClientOriginaLName());
}
如果问题routing
,我如何在routes
文件上注册这些routes
?我的意思是我必须使用什么动词以及每个函数内部的代码?或者还有其他一些可能的解决方案吗?感谢。
答案 0 :(得分:2)
我也遇到了Blueimp.jquery文件上传+ Laravel 4的错误。我通过使用PUT方法为我的Ajax调用添加路由来解决它。 (即使说jquery-file-upload使用POST in their documentation)
我的路线如下:
Route::put('upload/image', array('as' => 'admin.upload', 'uses' => 'App\Controllers\Admin\ImageController@postUpload'));
javascript电话:
$('.image-upload').fileupload({
url: '/upload/image',
dataType: 'JSON',
//type: 'POST',
done: function (e, data) {
//console.log(data);
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo($('.image-placeholder'));
});
}
});
修改强>
经过一些进一步的测试后,我发现如果我在我的请求中添加额外的'data.formData'(如here所述),它将使用POST方法而不是PUT ......
答案 1 :(得分:0)
MethodNotAllowedHttpException通常表示您用于调用该URL的方法不正确。
假设当您尝试上传文件时发生此错误,那么可能只是将该路由更改为POST路由而不是GET路由。
答案 2 :(得分:0)
这对我有用:
$('.image-upload').fileupload({
url: '/upload/image',
dataType: 'JSON',
method: 'POST',
done: function (e, data) {
//console.log(data);
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo($('.image-placeholder'));
});
}
});
然后你会收到csrf错误,可以修复添加csrf-field ...检查这里: StackOverFlow