下载zip工作从html表单,但不是从ajax

时间:2015-10-12 14:13:26

标签: javascript php jquery html ajax

我有这个代码并且工作正常

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);
}

我收到了一个zip文件。但我想知道什么时候下载所有图像,所以我试图使用jsonp的ajax。我使用相同的PHP代码。

我想用ajax帖子替换html表单。我现在有这个html和javascript(ajax post)而不是html post表单。 php脚本我还是一样。

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);
         }
    });
});

我没有使用ajax方法获取任何zip文件,而ajax给了我“错误:未定义”。如果我注释掉zip文件代码但保留将图像下载到临时文件夹的代码,我会获得“成功”。我的php脚本与站点(html / javascript)不在同一台服务器上。我在我的php文件中得到了回复

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

如果我注释掉我的zip功能。

0 个答案:

没有答案