file jquery laravel テスト
Route::post( /import [
as => import
uses => ArticlesController@import
]);
<h4>CSVファイルを選択してください</h4>
<form role="form" method="post" action="{{ route( import ) }}" enctype="multipart/form-data">
{{ csrf_field() }}
<table>
<tr>
<td>
aa{{ $selected_csv_file }}aa
<div class="form-group">
<input id="input-ja" class="file" type="file" name="csv_file" multiple class="file-loading" value="ccccc">
<input type="hidden" name="name" value="national_holiday" type="file">
</div>
<button type="submit" class="btn btn-primary" id="button2">インポートする</button>
</td>
</tr>
</table>
<input type="hidden" id="button_type" name="button_type">
<input type="hidden" id="selected_csv_file" name="selected_csv_file" value="{{ $selected_csv_file }}" type="file">
<input type="hidden" id="selected_file_object" name="selected_file_object" type="file">
<script>
$( #button2 ).on( click function() {
alert("クリックされました2 csv_import");
$( #button_type ).val("csv_import");
});
$("#input-ja").fileinput({
language: "ja"
allowedFileExtensions: ["jpg" "png" "gif" "csv"]
});
public function import(Request $request){
// 初期設定
$articles = [];
$contents ="" ;
$data = [] ;
$tableheader = [] ;
$csv_file1 = "" ;
// $file = $request->file( csv_file ); // UploadedFile {#167 ▼ -test: false -originalName: "a1.csv" -mimeType: "application/octet-stream" -size: 76 -error: 0 path: "C:¥xampp¥tmp" filename: "php71BA.tmp" basename: "php71BA.tmp" pathname: "C:¥xampp¥tmp¥php71BA.tmp" extension: "tmp" realPath: "C:¥xampp¥tmp¥php71BA.tmp" aTime: 2017-09-24 18:30:49 mTime: 2017-09-24 18:30:49 cTime: 2017-09-24 18:30:49 inode: 0 size: 76 perms: 0100666 owner: 0 group: 0 type: "file" writable: true readable: true executable: false file: true dir: false link: false linkTarget: "C:¥xampp¥tmp¥php71BA.tmp" }
//dd( $file->getRealPath() ) ; // "C:¥xampp¥tmp¥phpA4BE.tmp"
//dd($request->input( button_type ) );
if ( $request->input( button_type ) == "csv_import") {
//$file = $request->file( csv_file )->getRealPath();
// 指定の仕方がまちがってた。file( csv_file )が正しい。file( file )がだめだった
$file = $request->selected_csv_file;
//dd($file);
// $file = $request->file( selected_csv_file );
// $file = $request->file( csv_file ); // UploadedFile {#167 ▼ -test: false -originalName: "a1.csv" -mimeType: "application/octet-stream" -size: 76 -error: 0 path: "C:¥xampp¥tmp" filename: "php71BA.tmp" basename: "php71BA.tmp" pathname: "C:¥xampp¥tmp¥php71BA.tmp" extension: "tmp" realPath: "C:¥xampp¥tmp¥php71BA.tmp" aTime: 2017-09-24 18:30:49 mTime: 2017-09-24 18:30:49 cTime: 2017-09-24 18:30:49 inode: 0 size: 76 perms: 0100666 owner: 0 group: 0 type: "file" writable: true readable: true executable: false file: true dir: false link: false linkTarget: "C:¥xampp¥tmp¥php71BA.tmp" }
$model_name = App¥¥ .$request->name;
$rows = Excel::load($file->getRealPath() function($reader){
// $rows = Excel::load("./csv/a1.csv" function($reader){ //okokok
})->get();
$rows = $rows->toArray();
foreach ($rows as $row){
$model_name::create($row);
}
return view( articles.index );
} else {
//---------------------------------------------------
// csvファイル表示時
//---------------------------------------------------
echo "aaa2 csvファイル select指定あり" ;
$file = $request->file( csv_file );
// $file = $request->selected_csv_file;
//dd($file);
$reader = Excel::load($file->getRealPath());
$rows = $reader->toArray();
$kk = array_keys($rows[0]);
$tableheader[0] = $kk[0] ;
$tableheader[1] = $kk[1] ;
$tableheader[2] = $kk[2] ;
foreach ($rows as $key => $row){
// データ変換必要になる可能性があるため、その場合はここで処理
$data[$key][ date ] = $row[ date ] ;
$data[$key][ date_type ] = $row[ date_type ] ;
$data[$key][ name ] = $row[ name ] ;
}
$selected_csv_file = $file ;
return view( articles.index )
->with(compact( selected_csv_file ))
->with(compact( articles ))
->with(compact( tableheader ))
->with(compact( data ));
}
}
}