Jquery 将django HttpResponse作为文件返回,而不是在$.ajax()post中返回普通响应体
我正在尝试生成报告并将其作为文件返回给请求客户端: 客户端非常简单:Jquery 将django HttpResponse作为文件返回,而不是在$.ajax()post中返回普通响应体,jquery,ajax,django,http-headers,django-views,Jquery,Ajax,Django,Http Headers,Django Views,我正在尝试生成报告并将其作为文件返回给请求客户端: 客户端非常简单: $.ajax({ url: '/reports-get/', data: data_to_submit, type: 'GET', statusCode: { 200: function(e){}, 500: funct
$.ajax({
url: '/reports-get/',
data: data_to_submit,
type: 'GET',
statusCode: {
200: function(e){},
500: function(e){}
}
});
服务器端是:
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('UTF-8')), result, encoding='UTF-8', link_callback=fetch_resources)
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
result.close()
我正在用Chrome/FF进行测试,结果是:
我看到以下答复:
Request URL:http://localhost:8000/reports-get/?csrfmiddlewaretoken=e70579b0ad62d3744133dec4d51c98cc&delivery_type=pdf&report_name=consolidated_report&group_by=managers&projects_to_display=all
Request Method:GET
Status Code:200 OK
和响应标题:
Content-Disposition:attachment; filename=consolidated report 2012-04-03 grouping by managers2012-04-03-02-02.pdf
Content-Encoding:gzip
Content-Length:83312
Content-Type:application/pdf
Date:Mon, 02 Apr 2012 22:02:48 GMT
Server:WSGIServer/0.1 Python/2.7.1
Vary:Accept-Encoding, Cookie
以及响应正文中的文件:
%PDF-1.4
%���� ReportLab Generated PDF document http://www.reportlab.com
% 'BasicFonts': class PDFDictionary
1 0 obj
...
startxref
184036
%%EOF
但它不会保存文件,也不会提示我是否要保存文件。有什么我遗漏的吗?一旦你以编程方式请求它,浏览器就会闪开,否则你会有各种恶劣的行为。一种将其视为下载的方法是制定所需的URL,然后为其设置
window.location
。一旦您以编程方式请求它,浏览器就会闪开,否则您会出现各种恶劣行为。将其视为下载的一种方法是制定所需的URL,然后为其设置window.location
。您是否设置了正确的mime类型?我想是这样:response=HttpResponse(result.getvalue(),mimetype='application/pdf')
Oops的可能重复,似乎都是关于Ajax的。双引号文件名。您是否设置了正确的mime类型?我想是这样:response=HttpResponse(result.getvalue(),mimetype='application/pdf')
Oops的可能重复,似乎都是关于Ajax的。双引号文件名也。