从ajax post请求下载jquery的zip文件
我想知道是否可以对特定url发送ajax post请求,并仅在请求时接收数据中的zip文件? 或者我必须发送两个请求…一个,为了将zip文件的url放在已创建的服务器中,另一个请求下载zip文件?
本机答案是否定的!
但你可以这样做。
你的ajax请求:
$.ajax({ url: 'your-url-that-gives-zip-file.php', dataType: 'JSON', success: function(response){ if(response.zip) { location.href = response.zip; } } });
你的php文件:
$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();
你不能用ajax下载文件。
如果你只想要一个请求,那么抛弃ajax并将一个隐藏的iframe附加到页面上,以php文件为源。
这会限制您获取请求。
例如:
$('#id').click(function(){ $(body).append(''); });