简体   繁体   中英

upload file didn't work in laravel 5.1

I want to upload a .CSV file to insert some records to my Database.

Unfortunately, it doesn't work.

My Routes:

Route::get('excel/import','Backend\ExcelController@getImport');
Route::post('excel/import','Backend\ExcelController@postImport');

My Controller:

public function getImport(){

    $csrf_field = csrf_field();
    $postUrl='import';
    $html = <<<CREATE
    <form action="$postUrl" method="post">
        $csrf_field
        <input type="file" name="importCsv" formenctype="multipart/form-data" ><br/><br/>

        <input type="submit" value="submit"/>
    </form> 
CREATE;
   return $html;
}


public function postImport(Request $request){
    //get file
    $file = Input::file('importCsv');
    dd($file);
   $upload=$request->file('importCsv');
   dd($upload);
}

It just print null if I use :

Input::file('importCsv');<br>

And nothing if I use :

$request ->file('importCsv');<br><br>

So, I've tried to print like dd($request->all()); <br> dd($request->all()); <br>

array:2 [▼
"_token" => "wPuAXUvSItR4MFJ4bAjQhanaf0W9avrqR2PgjxcU"
"importCsv" => "T_OpusDef.csv"
]

I could get only the name, and when I want the the path by getRealPath() , It told me :

FatalErrorexception: call to a memeber function getRealPath() on null

I need your help, Thanks a lot

The form should look like this:

<form action="{{ $postUrl }}" method="post" enctype="multipart/form-data">
    {{ $csrf_field }}
    <input type="file" name="importCsv"><br/><br/>
    <input type="submit" value="submit">
</form> 

如果在刀片服务器中, 使用{{csrf_field()}}而不是$ csrf_field;如果在控制器中,请使用csrf_field()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM