我想知道是否有可能做一个ajax post请求到一个特定的网址,并在数据中接收一个压缩文件只在请求?或者我必须发送两个请求..。一个是为了在已经创建的服务器中拥有zip文件的url,另一个是为了下载zip文件?
发布于 2014-05-15 19:25:58
原生答案是否定的!
但是你可以这样做。
您的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));
?>发布于 2014-05-15 20:46:32
你肯定能做到!但只有新的浏览器才支持这一点。
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();发布于 2014-05-15 19:24:23
你不能用ajax下载文件。
如果你只想要一个请求,那就抛弃ajax,把一个隐藏的iframe附加到页面上,并把php文件作为它的源。
不过,这会将您限制为get请求。
例如:
$('#id').click(function(){
$(body).append('<iframe style="display:none;" src="yourfile.php?key=value"></iframe>');
});https://stackoverflow.com/questions/23676748
复制相似问题