上传表单在正常路由中不起作用

时间:2016-04-20 18:07:10

标签: laravel

我正在学习Laravel 4.2版本我写了一个简单的表格,用这段代码上传文件:

<form action="{{url('form')}}" method="post" enctype="multipart/form-data">
    <input type="file" name="libro">
    <input type="submit" value="invia">
</form>

这是我的路线:

Route::get('form',function(){
return View::make('form');
});

Route::post('dati',function(){
$file=Input::get('libro');
return $file;
});

当我访问该路线时,我收到此错误:

&#13;
&#13;
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException thrown with message ""

Stacktrace:
#14 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210
#13 Illuminate\Routing\RouteCollection:methodNotAllowed in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:197
#12 Illuminate\Routing\RouteCollection:getOtherMethodsRoute in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:145
#11 Illuminate\Routing\RouteCollection:match in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1054
#10 Illuminate\Routing\Router:findRoute in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1022
#9 Illuminate\Routing\Router:dispatchToRoute in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1001
#8 Illuminate\Routing\Router:dispatch in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
#7 Illuminate\Foundation\Application:dispatch in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:745
#6 Illuminate\Foundation\Application:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Session/Middleware.php:72
#5 Illuminate\Session\Middleware:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php:47
#4 Illuminate\Cookie\Queue:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php:51
#3 Illuminate\Cookie\Guard:handle in /opt/lampp/htdocs/blog/vendor/stack/builder/src/Stack/StackedHttpKernel.php:23
#2 Stack\StackedHttpKernel:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:641
#1 Illuminate\Foundation\Application:run in /opt/lampp/htdocs/blog/public/index.php:49
#0 require_once in /opt/lampp/htdocs/blog/server.php:19
&#13;
&#13;
&#13;

&#13;
&#13;
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException thrown with message ""

Stacktrace:
#14 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210
#13 Illuminate\Routing\RouteCollection:methodNotAllowed in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:197
#12 Illuminate\Routing\RouteCollection:getOtherMethodsRoute in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:145
#11 Illuminate\Routing\RouteCollection:match in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1054
#10 Illuminate\Routing\Router:findRoute in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1022
#9 Illuminate\Routing\Router:dispatchToRoute in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1001
#8 Illuminate\Routing\Router:dispatch in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
#7 Illuminate\Foundation\Application:dispatch in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:745
#6 Illuminate\Foundation\Application:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Session/Middleware.php:72
#5 Illuminate\Session\Middleware:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php:47
#4 Illuminate\Cookie\Queue:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php:51
#3 Illuminate\Cookie\Guard:handle in /opt/lampp/htdocs/blog/vendor/stack/builder/src/Stack/StackedHttpKernel.php:23
#2 Stack\StackedHttpKernel:handle in /opt/lampp/htdocs/blog/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:641
#1 Illuminate\Foundation\Application:run in /opt/lampp/htdocs/blog/public/index.php:49
#0 require_once in /opt/lampp/htdocs/blog/server.php:19
&#13;
&#13;
&#13;

即使我重写了我的代码,因为我遇到了同样的问题:

Route::get('form',function(){
return View::make('form');
});

Route::post('dati',function(){
$file=Input::File('libro');
return $file;
});

也是这样:

Route::post('dati',function(){
$file=Input::file('libro')->getClientOriginalName();
return $file;
});

无论如何。我在Ubuntu 15.10上,我用php 5.6.11运行XAMPP灯堆。 apache2和MySQL。一切正常但不是这样。

1 个答案:

答案 0 :(得分:0)

对刀片使用HTML方法,试试这个:

{{ Form::open(array('url' => 'dati', 'files' => true)) }}
    {{ Form::file('libia') }}
    {{ Form::submit('invia!') }}
{{ Form::close() }}

如果您看到自己的表单,则您的操作是获取视图的网址,因此您可以更改为发布路径。

 <form action="{{url('dati')}}" method="post" enctype="multipart/form-data">
    <input type="file" name="libro">
    <input type="submit" value="invia">
</form>