Support the ongoing development of Laravel.io →
Forms Views Blade

Ajax $(document).ready(function(){ $('form').submit(function(event){

		event.preventDefault();
		var formData = new FormData($(this)[0]);
		$.ajax({
			headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') },
			url: "{{url('admin/voter/sub/storecsv')}}",
			data: formData,
			type: 'post',
			cache: false,
			xhr: function() {
				var xhr = new window.XMLHttpRequest();
				$("#csv_uploading").modal('show');
				xhr.upload.addEventListener("progress", function(evt) {
					if (evt.lengthComputable) {
						var percentComplete = evt.loaded / evt.total;
						percentComplete = parseInt(percentComplete * 100);
						$('.uploadProgressBar').attr('aria-valuenow',percentComplete).css('width',percentComplete + '%').text(percentComplete + '%');
						if (percentComplete === 100) {
							$("#csv_uploading").modal('hide');
						}

					}
				}, false);

				return xhr;
			},
			processData: false,
			contentType: false,

			success:function(data){
				if(data.success === true){
        				LOAD VIEW ON SUCCESS WITH DATA similar to return view('path',compact('data1','data2')) with laravel
				}
			}
		});

	});
});

Controller

$this->validate($request,[ 'import_file' => 'required', ]);

    $db_header_obj = new Voter();
    $db_header = $db_header_obj->getTableColumns();
    $path = $request->file('import_file')->getRealPath();
    $csv = Reader::createFromPath($path, 'r');
        $csv->setHeaderOffset(0); 
        $csv_header = $csv->getHeader(); 

        $sample_data = $csv->fetchOne();
        $sample_data = array_values($sample_data);


    $extension = $request->file('import_file')->getClientOriginalExtension(); 
    $filename = uniqid().'_'.time().'_'.date('Ymd').'.'.$extension;
    Storage::disk('local')->putFileAs('/files/voter/', $request->file('import_file'), $filename);

        $arr = [
            'action' => 'storeCSV',
            'table' => $this->table,
        ];
        $this->storeActivity($arr);

        return view('admin.voter.import_fields', compact( 'csv_header', 'db_header','sample_data','filename'));

What I have tried, with no success

return view('admin.voter.import_fields', compact( 'csv_header'``, 'db_header','sample_data','filename'))->render(); return response()->json(array('success' => true, 'html'=>$returnHTML));

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.