The JQuery(AJAX call):
$('#frm').validate({
rules: {
date1: {
required: true,
},
date2: {
required: true,
},
},
submitHandler: function(form) {
let formD = new FormData(form);
//formD.append(Report_id, $('#inp_2').val());
$.ajax({
url: form.action,
type: form.method,
data: formD,
processData: false,
contentType: false,
cache: false,
success:function(data)
{
toastr.success(data);
},
error:'',
})
}
});
Controller:
public function product_list_report(Request $request)
{
$validator = Validator::make($request->all(),[
"date1" => 'required',
"date2" => 'required',
]);
if($validator->fails()){return dd("Errored");}
$f = time().'_'.'mybin_report.csv';
$headers = array(
'Content-Type' => 'application/vnd.ms-excel; charset=utf-8',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Disposition' => 'attachment; filename='.$f,
'Expires' => '0',
'Pragma' => 'public',
);
$filename = public_path("files/".$f);
if(isset($request->inp_2))
{
$data = DB::table('products')
->select('products.id as product_id', 'products.name as product_name', 'products.format', 'products.code as product_code', 'products.description', 'products.created_at')
->orderBy('products.created_at', 'DESC')
->get();
$columns = array('Product_id', 'Name', 'Format', 'Code', 'Description', 'Created On');
if (!File::exists(public_path()."/files")) {
File::makeDirectory(public_path() . "/files");
}
//creating the download file
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
$handle = fopen($filename, 'w');
foreach ($data as $dt) {
fputcsv($handle, [$dt->product_id, $dt->product_name, $dt->format, $dt->product_code, $dt->description, $dt->created_at,]);
}
$response = array(
'status' => 'success',
'response_code' => 200,
'msg' => 'Successfully created',
);
}
echo json_encode($response);
fclose($handle);
return response()->download($filename, $f, $headers);
}
The following is th jquery toastr response which means the code is already done with the success part of the ajax call
then it comes back to the controller an when it executes return response()->download($filename, $f, $headers);
the file should get downloaded which is not happening, without the AJAX call the file was being downloaded and I can't figure out why it ain't working.
Any help will be appreciated.