dongsuishou8039 2015-05-15 10:29
浏览 85
已采纳

Laravel POST令牌不匹配异常(文件上传)

I have a piece of functionality that creates a popup with a bit of responsiveness to it (this works), I have this html code which is my popup box (it's using jquery dialog to show the popup but the form is just between my other html code, surrounded by a div that makes it invisible):

<form id="logform" method="POST"> 
<select id='logoption'>  
<option value="0"> Select logtype </option>
<?php 
//SQL in laravel style to get log types from database
$logtypes = DB::table('time_log_types')->get();

foreach ($logtypes as $logtype)
{
echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>';
}
?>
</select>
<div id="illness" style="display:none">
<p>End date: <input type="text" id="enddate"></p>
<p>Doctor's note: <input type="file" id="doctorsnote"></p>                                            
</div>                                           
<input type="button" value="submit" id="submitbutton" style="display:none" />
</form>

The submit button on this popup box does this (using jquery):

<script>
$(document).ready(function() {
    //File upload
$('#submitbutton').on('click', function() {
            var fd = new FormData(document.getElementById("logform"));
//fd.append("CustomField", "This is some extra data");
            $.ajax({
                url: ' ',
                type: 'POST',
                data: fd,                
                cache: false,
                contentType: false,
                processData: false
            });
        });
    });

</script>

I'm making sure my POST is actually going to the correct place by adding this route in my routes.php(I know I am posting to the correct place because I have checked the network tab when using Inspect element in firefox):

Route::post('calendar', 'LogController@upload');

Finally this is the code I have in my LogController:

<?php namespace App\Http\Controllers;

use Input;

class LogController extends Controller {

    public function upload() {       
        $file = Input::file('doctorsnote');
        $destinationPath = 'C://xampp/htdocs/proofofconcept/savedImages';
        // If the uploads fail due to file system, you can try doing public_path().'/uploads' 
        $filename = str_random(12);
        //$filename = $file->getClientOriginalName();
        //$extension =$file->getClientOriginalExtension(); 
        $upload_success = $file->move($destinationPath, $filename);

        if( $upload_success ) {
           return Response::json('success', 200);
        } else {
           return Response::json('error', 400);
        }
    }
}

The problem is when I click the submit button I get a 500 response and I logged this error message but I have no idea what to do with it:

> [2015-05-15 10:08:14] local.ERROR: exception
> 'Illuminate\Session\TokenMismatchException' in
> C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:46
> Stack trace:
> #0 C:\xampp\htdocs\proofofconcept\laravel\app\Http\Middleware\VerifyCsrfToken.php(17):
> Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #1 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #2 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(55):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #3 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #4 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(61):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #5 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #6 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(36):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #7 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #8 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(40):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #9 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #10 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(42):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #11 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #12 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #13 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(101):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #14 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(115):
> Illuminate\Pipeline\Pipeline->then(Object(Closure))
> #15 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(84):
> Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
> #16 C:\xampp\htdocs\proofofconcept\laravel\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
> #17 {main}

I have absolutely no idea what is going on, I created this code following guides from all around and as far as I can tell this should be working but I'm kind of new to all of this and I could really use some help figuring this out

EDIT: I added a new field to my form to include the csrf token like this:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

and now I get a different stacktrace:

[2015-05-15 11:21:23] local.ERROR: exception 'ReflectionException' with message 'Class App\Http\Controllers\LogController does not exist' in C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php:776
Stack trace:
#0 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php(776): ReflectionClass->__construct('App\Http\Contro...')
#1 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php(656): Illuminate\Container\Container->build('App\Http\Contro...', Array)
#2 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(644): Illuminate\Container\Container->make('App\Http\Contro...', Array)
#3 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(83): Illuminate\Foundation\Application->make('App\Http\Contro...')
#4 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->makeController('App\Http\Contro...')
#5 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php(204): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\Http\Contro...', 'upload')
#6 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php(134): Illuminate\Routing\Route->runWithCustomDispatcher(Object(Illuminate\Http\Request))
#7 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(701): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#8 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#9 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(141): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#10 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#12 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(703): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#13 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(670): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#14 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(628): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#15 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(214): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#16 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#17 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(141): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#18 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(43): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 C:\xampp\htdocs\proofofconcept\laravel\app\Http\Middleware\VerifyCsrfToken.php(17): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(55): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(61): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(36): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#27 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(40): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#32 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#33 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(115): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#34 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(84): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#35 C:\xampp\htdocs\proofofconcept\laravel\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#36 {main}  

EDIT: I moved my endpoint for the upload to a different controller and it's working fine now, now however it is complaining that my Input::file('doctorsnote') is a nonobject:

[2015-05-15 11:35:20] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function move() on a non-object' in C:\xampp\htdocs\proofofconcept\laravel\app\Http\Controllers\CalendarController.php:27
Stack trace:
#0 {main}  

EDIT: I now have this for my form but it's not registering when I click the submit button, the POST is not being executed

<form id="logform" enctype="multipart/form-data" action="calendar"  method="POST"> 
<select id='logoption'>  
<option value="0"> Select logtype </option>
<?php 
//SQL in laravel style to get log types from database
$logtypes = DB::table('time_log_types')->get();    
foreach ($logtypes as $logtype)
{
echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>';
}
?>
 </select>

<div id="illness" style="display:none">
<p>End date: <input type="text" id="enddate"></p>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<p>Doctor's note: <input type="file" name="doctorsnote" id="doctorsnote"/></p>                                            
                                            </div>                                            

<input type="button" value="submit" id="submitbutton" style="display:none" />
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
</form>
  • 写回答

2条回答 默认 最新

  • dongzhang7961 2015-05-15 14:28
    关注

    I just managed to upload a file :D HOW I went about it:

    STEP 1: edited my form Like this: - Added enctype, added an action to my form, added method post - Added hidden field for csrftoken - removed all javascript related to submitting my form (don't really need this ajax call)

    <form id="logform" enctype="multipart/form-data" action="calendar"  method="post"> 
    <select id='logoption'>  
    <option value="0"> Select logtype </option>
    <?php 
    //SQL in laravel style to get log types from database
    $logtypes = DB::table('time_log_types')->get();
    
    foreach ($logtypes as $logtype)
    {
    echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>';
    }
    ?>
    </select>
    
    <div id="illness" style="display:none">
    <p>End date: <input type="text" name="enddate" id="enddate"></p>
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <p>Doctor's note: <input type="file" name="doctorsnote" id="doctorsnote"/></p>                                            
    </div>    
    <input type="button" value="submit" id="submitbutton" style="display:none" onclick="submit()" />
    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
    </form>
    

    STEP 2: remove javascript

    STEP 3: Moved the upload function to my calendarController and changed the route to:

    Route::post('calendar', 'CalendarController@upload');
    

    I didn't change anything else (except remove the whole javascript function)

    and it works like a charm, took me an entire day to get it to work but I guess not bad for a complete novice :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?