我正在使用 ajax 向我的 django 函数发送请求,然后生成一个 zip 文件并将其提供给用户。如果我转到该 url domain.com/django/builder/zipit/
,文件会按预期生成并下载到我的计算机,但是当使用 ajax 并返回响应时,ajax 无法下载它。我可以将响应传递给 php 变量并以这种方式下载吗?使用 iframe 将不起作用,因为该文件是动态创建的。
阿贾克斯
$.ajax({
type: 'POST',
url: '/django/builder/zipit/',
data: serialize,
success: function(response){
//pass response to php somehow
}
视图.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