我正在使用Blueimp.jquery file-upload用于文件上传。我的应用程序也在 Laravel 4
框架下,我在上传文件时遇到错误。
此 API
在 Firefox 和 Chrome 中返回的错误是:
Error: Method Not Allowed
我认为错误是由 Laravel
上的 routing
引起的,因为这是我在 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());
}
如果路由
是问题所在,我如何在我的routes
文件上注册这些路由
?我的意思是我必须使用哪些动词以及每个函数内部的代码?或者还有其他可能的解决方案吗?谢谢。
最佳答案
我在 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...
关于jquery - BlueImp 文件上传 "Error: Method Not Allowed"- Laravel 4 路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108095/