0

I have this code and it's working fine

HTML

<form method="POST" action="http://example.com/downloadtest.php">
<button type="submit">Submit button</button>
</form>

PHP (downloadtext.php)

header("access-control-allow-origin: *");
$urls = array("www.domain.com/image1.jpg", "www.domain.com/image2.jpg");

//php code do download the images to a temp folder

function createZipfile($folderName) {
    // make sure to send all headers first
    // Content-Type is the most important one (probably)
    //
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename="myzip.zip"');

    // use popen to execute a unix command pipeline
    // and grab the stdout as a php stream
    // (you can use proc_open instead if you need to
    // control the input of the pipeline too)
    //

    $fp = popen('zip -r -j - tmp/'.$folderName, 'r');

    // pick a bufsize that makes you happy (8192 has been suggested).
    $bufsize = 8192;
    $buff = '';
    while( !feof($fp) ) {
        $buff = fread($fp, $bufsize);
        echo $buff;
    }
    pclose($fp);
}

I get a zip-file. But I want to know when all the images are downloaded so I am trying to use ajax insted with jsonp. I am using the same PHP code.

I want to replace the html form with ajax post. I now have this html and javascript (ajax post) instead of the html post form. The php script i still the same.

html

<button class="download-all-images">Download</button>

javascript

$(".download-all-images").click(function(){
    $.ajax({
         url:"http://example/downloadtest.php",
         type: "POST",
         dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
         success:function(json){
             alert("Success ");
         },
         error:function(e){
             alert("Error " + e.message);
         }
    });
});

I'am not getting any zip file with the ajax method and ajax gives me "Error: undefined". I get "Success" if I comment out the zip-file code but keep the code that downloads the images to a temp folder. My php script is not on the same server as the site (html/javascript). And I get a response with this in my php file

echo $_GET['callback'] . '('.json_encode($urls).')';

if I comment out my zip-function.

Xtreme
  • 1,601
  • 7
  • 27
  • 59
  • This is somewhat confusing? It looks like you have a PHP script that downloads a file directly to a users computer, and it's really not clear how you intend to do something like that with ajax ? – adeneo Oct 12 '15 at 14:19
  • Yes I'am downloading to the user. I want to inform the user before the zip download starts and remove that information when I have downloaded the images to the temp folder. I thought ajax with jsonp was the correct way. – Xtreme Oct 12 '15 at 14:22
  • Maybe it is, but I can't really see how ajax would help you download a file to the user, usually you do that with the code you have, or with the new HTML5 `download` attribute ? – adeneo Oct 12 '15 at 14:23
  • Ajax does not help me to download the file. Ajax helps me to know "where" in the php file I am. I can't inform the user with html form. What I am trying to do with ajax is display some (wait..) text when they click download. The files is downloaded to the tempfolder and I am creating a zip in my php file. When the zip is ready to download I want to remove the "wait..." information, (and it must be when my ajax is "success"). Found this http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain because my site and the php script is not on the same domain. – Xtreme Oct 12 '15 at 14:29
  • If this is possible they should patch it. Cause if you can push a file to a users computer without their manual action (click) it would be unsafe. I say push because the user does not need to initiate, the programmer/program could trigger that. So no, this should not be possible imo. That is to say if I understand you problem correct – Matt Oct 12 '15 at 14:30
  • Lets see if we understand this. When you click the button, the form redirects to the PHP script, but because of the content-disposition etc. it never redirects, it just starts downloading the file. At the same time you want to do an ajax call from the clientside to tell you when the download has finished? If that's it, it usually involves some issues with multiple requests in the same session, setting, checking and deleting cookies or something similar ? – adeneo Oct 12 '15 at 14:49
  • @adeneo, I think you misunderstand me. I've updated my question, which will hopefully make it clearer. My first two code blocks, the html post form and the php script works fine. The form redirect to a PHP script and I get a zip file downloaded with the images. I want to replace the html form `
    ` with ajax post. I have tried to replace the form tag with a `
    – Xtreme Oct 12 '15 at 18:49

0 Answers0