1

I am trying to build a function that exports and XML as download. I use the XMLWriter function from PHP which works great. I actually get the result I want, only, it comes back in my AJAX response rather than it's opted as a download, which i told the script to do so.

My layout is as followed:

JS + AJAX

// ///////////////////////////// //
// START EXPORT                  //
// ///////////////////////////// //

function exports(vars){

    $.ajax({

        url: domain + "/core/ajax/export.php",

        type: "post",

        dataType: "text",

        data: vars,

        success: function(data){

            if(vars.form == 'export-records'){



            }

        },

        error: function(jqXhr, textStatus, errorMessage){

            console.log("Error: ", errorMessage);

        }

    });

}

$('#export-propperties').bind('submit',function(){

    event.preventDefault();

    var content = $('#export-propperties').serializeArray().reduce(function (newData, item) {

        if (item.name.substring(item.name.length - 2) === '[]') {

            var key = item.name.substring(0, item.name.length);

            if(typeof(newData[key]) === 'undefined') {

                newData[key] = [];

            }

            newData[key].push(item.value);

        } else {

            newData[item.name] = item.value;

        }

        return newData;

    }, {});

    exports(content);

});

So what happens here is i submit my form as a key value pair into my ajax function. Works fine. The php proces is as follows.

        // START THE XML HERE
        // AFTER THE DATA IS FETCHED WE WILL PARSE IT NORMALLY. WE ARE GOING TO USE THE RESULT LATER, FIRST WE CREATE THE INSTANCE OF THE FILE
        // TO DEFINE FIRST THE XML

        $writer = new XMLWriter();  

        $writer->openURI('php://output');  

        $writer->startDocument('1.0','UTF-8');  

        $writer->setIndent(4);   


        // CREATE THE HEADING OF THE PRODUCTDATA

        $writer->startElement('ProductData');  

            $writer->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');  

            $writer->writeAttribute('xmlns', 'http://www.gs1.nl/productgegevens/insbou/004');  

            $writer->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');  

            $writer->writeAttribute('xsi:schemaLocation', 'http://www.gs1.nl/productgegevens/insbou/004 Productgegevens_insbou004.xsd');  

            // blablabla xml generation //

        $writer->endElement();  

        // END OF DOCUMENT

        $writer->endDocument();   

        $writer->flush();   

The problem is that my response of the code is XML, rather than the wanted XML download ($writer->openURI('php://output');). I have a feeling I cant execute the download window within the AJAX call, or am I wrong here? How can I solve this?

Dorvalla
  • 5,027
  • 4
  • 28
  • 45
  • does https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request answer your problem. – Nigel Ren Apr 01 '20 at 18:35
  • @NigelRen no sorry, it doesnt work that way cause my data only get send once to my file. If it reopens because of it is successfully executed, i dont have my variables on which my export happens. The trick does work, but isnt the solution i can work with. – Dorvalla Apr 01 '20 at 18:43
  • I removed for now the AJAX call out of it. Since i marked the target page as a download page in the header (which works) it sticks to the original page where my page is, so no harm done on this part. AJAX and downloads dont play nice – Dorvalla Apr 01 '20 at 19:04

1 Answers1

1

Maybe header('Content-Disposition: attachment; filename="filename.xml"'); in the top of your PHP code will work. I'm not sure, but give it a try.
Another solution is FileSaver.js.
Sorry for my English.

Zoldszemesostoros
  • 387
  • 1
  • 3
  • 15
  • removed the ajax call and added this to it, works good, thanks. I ll mark it as awnser, despite not being totally the right thing. A bit of googling helped me along that AJAX just doesnt play nice when it comes to downloading. – Dorvalla Apr 01 '20 at 19:05