1

I'm developing an SVG editor. I have to save the svg picture on the local disk. As you know, for safety reasons, it is impossible do it directly with javascript. So I decided to approach the problem with the server side help. I wrote the following PHP routine in a file called "savefile.php":

    <?php
$mime = array('xls' => 'application/vnd.ms-excel', 'xml' => 'application/xml', 'html' => 'text/html', 'cvs' => 'text/plain', 'svg' => 'text/plain', 'txt' => 'text/plain', 'json' => 'text/plain',  'array' => 'text/plain');

if (isset($_POST['format']) && isset($_POST['filename']) && isset($_POST['content'])) {
    $filename = $_POST['filename'];
    $format = $_POST['format'];
    $content = $_POST['content'];

    $fullName = $filename . '.' . $format;
    header('Pragma: public');
    header("Content-Description: File Transfer");
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header("Cache-Control: public");
    header('Content-Type: ' . $mime[$format]);
    header('Content-Disposition: attachment; filename="' . basename($fullName) . '"');
    echo $content;
}
?>

On the server side I call, the PHP procedure, with this code:

        var obj = {
            format: 'svg',
            filename: 'myfilename',
            content: glb.Draw_active().GetDisegno().svg()
            };
        $.post("php/savefile.php",obj,function(data,status,xhr){console.log('Executed');});

When the software execute the above code, the browser should open the savefile window and wait the confirm from the user..... but nothing happens. I'm sure that the PHP routine is executed, it is also executed the callback routine, but nothing else. Something is wrong, my suspect is on the javascript client-side code but I'm not able to find any documentation. May be someone have experience on this procedure?

Thanks a lot.

Gildo
  • 43
  • 6
  • Duplicate: http://stackoverflow.com/questions/24044961/unable-to-download-excel-file-using-ajax-jquery – Quentin Mar 11 '16 at 08:18
  • Thanks for your answer, I red that questions but, may be because I'm novice for this things, I didn't understand whats wrong in my code. May be you can help me? Thanks – Gildo Mar 11 '16 at 11:06
  • "I didn't understand whats wrong in my code" — You are using Ajax. That is what is wrong. – Quentin Mar 11 '16 at 11:07

1 Answers1

0

Thanks to @Quentin for the help I have solved the problem. The PHP code above is working, I had to change the Javascript part. Here the working code:

The SaveFile routine:

/*
    @datatype       : 'svg', 'xml', 'txt', 'xls', 'csv', 'html', etc see the list in PHP file
    @filename       : filename without extension
    @exportServer   : name of the PHP file on the server
    @content        : content of the file to save
*/
var saveFile = function(datatype, filename, exportServer,content){
    var HInput = function(value, name, form) {
        var I = document.createElement('input');
        I.name = name;
        I.value = value;
        I.type = 'hidden';
        form.appendChild(I);
        },
    HTextArea = function(value, name, form) {
         var tA = document.createElement('textarea');
         tA.name = name;
         tA.value = value;
         form.appendChild(tA);
         };
    var form = document.createElement('form');
    HInput(filename, 'filename', form);
    HInput(format, 'format', form);
    HTextArea(content, 'content', form);
    form.action = exportServer;
    form.method = 'post';
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
    }

.... for my need I call it in this way:

saveFile('svg','myfilename',"php/savefile.php",data);

thats all...... ;)

Gildo
  • 43
  • 6