Php 从ajax post请求下载带有jquery的zip文件

Php 从ajax post请求下载带有jquery的zip文件,php,jquery,ajax,Php,Jquery,Ajax,我想知道是否有可能对特定的url执行ajax post请求,并仅在请求时接收zip文件中的数据?或者我必须发送两个请求。。。一个是为了在已创建的服务器中拥有zip文件的url,另一个是为了下载zip文件?您不能使用ajax下载文件 如果您只需要一个请求,那么就放弃ajax,在页面上附加一个隐藏的iframe,并将php文件作为其源文件 但这将限制您执行get请求 例如: $('#id')。单击(函数(){ $(正文)。附加(“”); }); 本机答案是否定的 但你可以这样做 您的ajax请求:

我想知道是否有可能对特定的url执行ajax post请求,并仅在请求时接收zip文件中的数据?或者我必须发送两个请求。。。一个是为了在已创建的服务器中拥有zip文件的url,另一个是为了下载zip文件?

您不能使用ajax下载文件

如果您只需要一个请求,那么就放弃ajax,在页面上附加一个隐藏的iframe,并将php文件作为其源文件

但这将限制您执行get请求

例如:

$('#id')。单击(函数(){
$(正文)。附加(“”);
});

本机答案是否定的

但你可以这样做

您的ajax请求:

$.ajax({
    url: 'your-url-that-gives-zip-file.php',
    dataType: 'JSON',
    success: function(response){
        if(response.zip) {
            location.href = response.zip;
        }
    }
});
您的php文件:

<?php

//Get your zip file code with and combine with http://youradress.com

$zipFile = 'http://youraddress.com/downloads/'.$zipFile;

echo json_encode(array('zip' => $zipFile));

?>

你一定能做到!但只有新浏览器支持这一点

var url = 'test.zip';
var filename = 'test.zip';
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'blob';
request.onload = function() {
   var link = document.createElement('a');
   document.body.appendChild(link);
   link.href = window.URL.createObjectURL(request.response);
   link.download = filename;
   link.click();
};
request.send();

当文件大小达到几兆字节时,浏览器是否有可能崩溃?用户会看到下载进度吗?我猜他不会的-如果我得到正确的解决方案…@SimonSimCity check out library,这会让你对尺寸限制有一些了解。您可以通过xhr进度事件跟踪下载进度,请参阅thxx man…u按天保存…已使用约2.5小时…再次使用thxx
var url = 'test.zip';
var filename = 'test.zip';
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'blob';
request.onload = function() {
   var link = document.createElement('a');
   document.body.appendChild(link);
   link.href = window.URL.createObjectURL(request.response);
   link.download = filename;
   link.click();
};
request.send();