0

I'm having some issues regarding this php library which is not working as i want to, i hope you can help me out with this one.

My program first calls a service through ajax which returns a .json.Then with the given.json i need to save it in a xls excel file, for this I'm using another ajax which calls to php service to save it locally (generarExcel2.php)

Below is the js/ajax part of the code in my html which actually works fine.

    $(document).ready(function () {
        var exceljsondatos;
        var path ="{{urlSf}}";
        $.ajax({
            type: "POST",
            url: path,
            dataType: 'json',
            success: function (data) { 
                exceljsondatos = data;
                $('#genExcel').click(function () {
                    $.ajax({
                        type: "POST",
                        url: "generarExcel2.php",
                        data: exceljsondatos,
                        success: function (resultado) {
                            $('#resp').html(" Function call successful"); }
                    });//End exel ajax call
                });//End click event
            }//End of success
        });//End ajax data call
    }); //End of  document ready

This shows on my html the correct message, so seems to work in this side. On my php i've checked its recieving the $_POST content correctly through var_dump but here comes the main problem.

I cant manage to prompt the user to save the data. I've tried using writer and with IOFactory, none works Here is the php code so you can take a look.

   <?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;//Option1
//use PhpOffice\PhpSpreadsheet\Writer\Xls;//Option2
$filename="Excel_jano_test";
$spreadsheet = new Spreadsheet(); 
$excel_writer = IOFactory::createWriter($spreadsheet, 'Xls');//Option1
//$excel_writer = new Xls($spreadsheet); //Option2
$activeSheet = $spreadsheet->getActiveSheet();    
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $filename .'.xls"');
header('Cache-Control: max-age=0');

$activeSheet->setCellValue('A1' , 'NUM.SOLICITUD');//example for dummy
$excel_writer->save('php://output');//THIS SHOULD PROMPT USER

I've tried changing last line to $excel_writer->save('otherfile.xls'); and it saves changes CORRECTLY on server. If you can see what i cant i will be grateful. Thanks a lot!

One more thing. I've checked that if i click on the route such as actually works and PROMPTS user but this way it wont be passing or recieving data.

Mbotet
  • 170
  • 2
  • 17

1 Answers1

0

"It's not possible to force downloading when using AJAX." Found this answer for similar problem here: https://stackoverflow.com/a/10191727/10551975

Anyway i still think there might be a possible workaround which would be more than welcome. I'll leave this open a few more days just in case someone can help me further to give him credit.

Edit: I ended up using an empty form which had the data loaded with ajax and then using regular submit button. Not what i wanted but works fine.

Mbotet
  • 170
  • 2
  • 17