2016-04-20 40 views
0

我學習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; 
}); 

當我參觀路線我得到這個錯誤:

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

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

即使我重寫我的代碼,這個我有同樣的問題:

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。一切正常,但不是這樣。

+0

我將返回response-> download($ file,$ filename,$ headers);更重要的是我會def使用控制器'$ headers = array('Content-Type:application/pd'); 返回響應::下載($文件,$文件名,$頭);' –

+0

謝謝我解決了。我的錯誤是在路線後。感謝您的分享 –

回答

0

使用HTML對葉片的方法,試試這個:

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

,如果你看到你的表格,你的動作是URL來獲取視圖,這樣你就可以改變後的路線。

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

感謝@ maytham-ɯɐɥʇʎɐɯ 我已經編輯我的形式,因爲這 {{表::打開(陣列( 'URL'=> '發送', '文件'=>真)) }} {{表::標籤(單擊 '文件', '發送文件')}}
{{表::文件( '書')}} {{表::提交('送')}} {{表::接近()}} 路線::獲得(' 發送」,函數(){ return View :: make('form'); }); Route :: post('dati',function(){ return Input :: file('book') - > getClientOriginalName(); }); 我有同樣的問題 –

+0

更改您的窗體中的網址打開,您的路線必須在這裏將文件發送到哪裏dati –

+0

謝謝我做到了。問題解決了 –