【发布时间】:2014-07-18 13:43:07
【问题描述】:
我正在尝试实现以下目标:在客户端,使用 Jquery/Ajax,我向我的 Django 服务器发出 Post 请求。在服务器端,我收到此请求,提取参数,基于此确定文件路径,然后构建 HttpResponse ,目的是让浏览器下载该文件。
但这并没有发生,尽管我在响应中看到了附件的内容。
响应构建为:
response = HttpResponse(file(path_to_file))
response['Content-Type'] = 'application/force-download'
response['Content-Length'] = os.path.getsize(path_to_file)
response['Content-Disposition'] = 'attachment; filename=\"request.txt\"'
response['Accept-Ranges'] = 'bytes'
return response
这是使用 firebug 看到的响应标头
Accept-Ranges bytes
Connection keep-alive
Content-Disposition attachment; filename=grequest.txt
Content-Length 228
Content-Type application/force-download
Date Fri, 18 Jul 2014 14:55:33 GMT
Server nginx/1.4.4
Set-Cookie sessionid=41602cd107bbddb41e8884a88c9035c0; Path=/
Vary Authorization, Cookie
这是响应内容,用萤火虫看到
eyJub2RlSUQiOiIwMmI1ODMtYjNhMTljLWM1MjkwYi05YzAwIiwiYWxpYXMiOiJsb2NhbGhvc3QiLCJkbnNOYW1lIjoibG9jYWxob3N0IiwiY2hhc3Npc1NlcmlhbCI6IiIsImFjdGl2YXRpb25zIjpbeyJhY3RpdmF0aW9uSUQiOiI3RjE3LUZFRUQtMTI5Ny1GOTUyIiwicXVhbnRpdHkiOiIzIn1dfQ==
这里是附件的内容
关于我做错了什么有什么建议吗?
【问题讨论】: