开始自动下载由Ajax制作的zip

时间:2017-03-07 09:50:46

标签: php jquery html

我有一个按钮让服务器通过Ajax生成zip文件。

$('.zipButton').click(function(){
    console.log($(this).context.firstChild.data);
    $(this).context.firstChild.data = "working......";


    $(this).addClass('disabled');
    var self = $(this);

    $.post('{{path('acme_member_zip')}}',// make zip file on server.
        function(response){
            if(response.code == 100 && response.success){
                console.log(response.message);  
                self.context.firstChild.data = "finish make zip";
                alert("please download http://www.myserver.com/myzip/arc.zip");             
            }   
            else {console.log("no correct return");} 
        }, "json");
});  

目前,在制作zip之后,我让用户通过提醒知道zip文件的位置。

但是,我想推动浏览器自动开始下载。 我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以将客户端重定向到document.location的ZIP网址,如下所示:

$.post('{{path('acme_member_zip')}}',// make zip file on server.
    function(response){
        if(response.code == 100 && response.success){
            console.log(response.message);  
            self.context.firstChild.data = "finish make zip";
            document.location = "http://www.myserver.com/myzip/arc.zip";         
        }   
        else {
            console.log("no correct return");
        } 
    }, 
    "json"
);

答案 1 :(得分:1)

实现这一目标的方法很少。

1) 您可以使用超链接

中的_design/t-cust-design/_view/by-custdes属性

download

<a href="your file path" download id="download">Your file Name</a>

2) 使用jQuery文件下载插件 https://github.com/johnculviner/jquery.fileDownload

3) 成功后设置document.getElementById('download').click();