0

Edit: I have a solution of sorts, but I do not understand why ajax is not working. Please see the edit below.

  1. I want to save a txt file generated in javascript to the client machine.
  2. I want the user to remain on the same webpage.
  3. I am doing it via ajax with a call to a php page.
  4. I want it to work in all major pc browsers; Chrome, Firefox, Internet Explorer and Edge.

Here is what I have so far:

htmlsave.js

dataARR = {
    SaveFile: "This is the content.\nLine2.\nLine3"
};
var dataJSON = JSON.stringify(dataARR);
$.ajax({
    type: "POST",
    url: "htmlsave.php",
    async: true,
    cache: false,
    crossDomain: false,
    data: { myJson: dataJSON },

    success: function (data) {
        alert("success");
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("failure");
    }
});

htmlsave.php

<?php
if(!isset($_POST) || !isset($_POST['myJson'])) {
    exit(0);
}
$data = json_decode($_POST['myJson'], true);
if(!array_key_exists('SaveFile', $data) || !isset($data['SaveFile'])){
    exit(0);
}
$contents = strval($data['SaveFile']);

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"mysavefile.txt\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($contents));
ob_clean();
flush();
echo $contents;
?>

What comes back (ie the data argument in success) is "This is the content.\nLine2.\nLine3".

There is no prompt to save the file with content "This is the content.\nLine2.\nLine3" as I had hoped.

Edit: A solution (but not a great one)

I have done a bit of tinkering and have something that works, in a fashion. Instead of calling through ajax I write in my javascript

document.location = "htmlsave.php?myJson=Line1Line2";

and in my php I have altered it to a get with

if(!isset($_GET) || !isset($_GET['myJson'])) {
    exit(0);
}
$contents = $_GET['myJson'];

This works. This will be ok for small files, but I wanted to do reasonably large text files, and even zipped using lzw encoding. Anyone know why Ajax is not working?

Rewind
  • 2,554
  • 3
  • 30
  • 56

1 Answers1

0

Make sure your htmlsave.php starts with

<?php

If it does, see if your webserver is configured to pass .php files to your php implementation (fpm, mod_php, etc...)

Xyz
  • 5,955
  • 5
  • 40
  • 58
  • I have edited the question to put in the – Rewind Jun 03 '16 at 13:28
  • Then it is likely an issue with you web server not handling off the file to the php parser. What web server are you running and which php version? – Xyz Jun 03 '16 at 13:31
  • I am using wamp server 64 bit version 3 (the latest) on windows 10. – Rewind Jun 03 '16 at 13:45
  • thanks for the response. How do I check, "Then it is likely an issue with you web server not handling off the file to the php parser." – Rewind Jun 03 '16 at 14:18
  • I really can't help with configuration of wamp, but you try to enter the url to htmlsave.php in a browser or curl and tail your web server logs. First, make sure if you'll get the plain php file in your browser so that this is the correct case, and then make a new question on an appropiate forum (maybe http://webmasters.stackexchange.com/). – Xyz Jun 03 '16 at 14:51
  • I have tested in on the wamp localhost in the browser writing 'localhost:8080/mytest/htmlsave.php?myJson=Line1Line2' into the address (altering the php file for a GET statement). This works fine. It fails when I do the call with AJAX from the html page. I have tested I can call php files from my javascript using AJAX (without a forced download) and that also works fine. For some reason it does not like the forced download when called through AJAX. – Rewind Jun 03 '16 at 15:43
  • I have done a bit of tinkering and have something that works, in a fashion. Instead of calling through ajax I write in my javascript document.location = "htmlsave.php?myJson=Line1Line2"; This will be ok for small files, but I wanted to do reasonably large text files, and even zipped using lzw encoding. Anyone know why Ajax is not working? – Rewind Jun 03 '16 at 16:01