【发布时间】:2018-07-18 06:21:00
【问题描述】:
我正在发出 jquery ajax POST 请求并将数据传递到 Django 框架中的后端 python 代码。我需要将 zip 文件作为 httpresponse 返回,它应该会在浏览器中自动下载。
object_body = request.body.decode('utf-8')
loaded_json = json.loads(object_body)
json_obj = ast.literal_eval(str(loaded_json))
zip_file_path = importexport.download(json_obj,user)
zip_filename = os.path.basename(zip_file_path)
with open(zip_file_path, 'rb') as zip:
response = HttpResponse(zip.read(), content_type = 'application/x-zip')
response['Content-Disposition'] = 'attachment; filename=' + zip_filename
return response
【问题讨论】:
-
很好的问题,到目前为止,您的代码结果如何?您收到错误消息还是没有收到附件?
-
目前你有什么?任何回应?有输出吗?
-
文件未作为附件下载。作为响应,它显示 zip 文件内容
-
这是正在传递的 ajax 请求 ....return $.ajax({ url: "/testmgr/importexport/", data: JSON.stringify(data), method: 'POST', dataType: 'application/x-zip', 接受: 'application/x-zip', 成功: function(response) { }, });