我使用ajax向我的django函数发送请求,然后生成一个zip文件并将其提供给用户。如果我去url domain.com/django/builder/zipit/
文件如预期生成并下载到我的电脑,但使用ajax和响应返回时,ajax不能下载它。我可以将响应传递给一个php变量并以这种方式下载吗?使用iframe不会工作,因为该文件是动态创建的。django将ajax响应传递给php变量
AJAX
$.ajax({
type: 'POST',
url: '/django/builder/zipit/',
data: serialize,
success: function(response){
//pass response to php somehow
}
views.py
def send_zipfile(request):
temp = tempfile.TemporaryFile()
archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
filename = '/home/dbs/public_html/download/video.html'
archive.write(filename, 'file.html')
archive.close()
wrapper = FileWrapper(temp)
response = HttpResponse(wrapper, content_type='application/zip', mimetype='application/x-download')
response['Content-Disposition'] = 'attachment; filename=dbs_content.zip'
response['Content-Length'] = temp.tell()
temp.seek(0)
return response
PHP与它有什么关系? –
http://bytes.com/topic/javascript/answers/610641-file-download-ajax – Menztrual
你的回应内容是什么? –