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条)

报告相同问题?

悬赏问题

  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码: