我正在尝试生成报告并将其作为文件返回给请求客户端:
客户端非常简单:
$.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
但它没有保存文件,如果我愿意也没有提示我。有什么我想念的吗?
答案 0 :(得分:4)
只要您以编程方式请求它,浏览器就会走开,或者您会遇到各种令人讨厌的行为。将其视为下载的一种方法是制定您需要的URL,然后将window.location
设置为它。