I'm using Reportlab with Django to generate pdf of data computed on the client side.
我正在使用Django的Reportlab生成客户端计算的数据的pdf。
Ajax is chosen because the client has non-trivial data to transfer for pdf generation.
之所以选择Ajax,是因为客户机有大量数据要传输给pdf生成。
$.ajax({
type:'POST',
url:document.URL + '/export',
data: JSON.stringify(data),
dataType:'json',
contentType:'application/pdf',
success:function(data){
// Question 1. What should I insert here to download pdf?
},
error:function(e){
...
}
});
And here is the view.py
这是view。py
def export(request, *args, **kwargs):
// Perform Ajax check
...
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="essay.pdf"'
p = canvas.Canvas(response, bottomup=0)
data = json.loads(request.body)
p.drawString(100, 100, "Hello world.")
p.showPage()
p.save()
return response
Question 2. I could not get ajax to succeed with its request, only invoking error callback. The same issue was referred in this question
问题2。我无法让ajax成功地完成它的请求,只调用错误回调。在这个问题中也提到了同样的问题
在Django ReportLab
but it wasn't answered. Am I missing something?
但它不是回答。我遗漏了什么东西?
2 个解决方案
#1
1
my solution would be to use a standard form instead of ajax.
我的解决方案是使用标准表单而不是ajax。
You can put a form in your html (or build it using JS)
可以在html中放置表单(或者使用JS构建表单)
<form id="my_form" action="export" method="post">
<input id="my_form_data" type="hidden" name="data" />
</form>
Then, when you want your data sent, using JS:
然后,当您希望发送数据时,使用JS:
$('#my_form_data').value(JSON.stringify(data));
$('#my_form').submit();
#2
1
You can't download a file via ajax. But you can use this library: jquery.fileDownload.
不能通过ajax下载文件。但是您可以使用这个库:jquery.fileDownload。
jQuery File Download is a cross server platform compatible jQuery plugin that allows for an Ajax-like file download experience that isn’t normally possible using the web.
jQuery文件下载是一个跨服务器平台兼容的jQuery插件,它允许类似ajax的文件下载体验,而通常使用web是不可能的。
#1
1
my solution would be to use a standard form instead of ajax.
我的解决方案是使用标准表单而不是ajax。
You can put a form in your html (or build it using JS)
可以在html中放置表单(或者使用JS构建表单)
<form id="my_form" action="export" method="post">
<input id="my_form_data" type="hidden" name="data" />
</form>
Then, when you want your data sent, using JS:
然后,当您希望发送数据时,使用JS:
$('#my_form_data').value(JSON.stringify(data));
$('#my_form').submit();
#2
1
You can't download a file via ajax. But you can use this library: jquery.fileDownload.
不能通过ajax下载文件。但是您可以使用这个库:jquery.fileDownload。
jQuery File Download is a cross server platform compatible jQuery plugin that allows for an Ajax-like file download experience that isn’t normally possible using the web.
jQuery文件下载是一个跨服务器平台兼容的jQuery插件,它允许类似ajax的文件下载体验,而通常使用web是不可能的。