from applications.ChartGenerator.modules import generator
from gluon import HTTP, contenttype
#URL
def generate():
if request.env.request_method == 'POST':
import gluon.contrib.simplejson
try:
data = gluon.contrib.simplejson.loads(request.body.read())
file_path = generator.go(data)
response.headers['Content-Disposition'] = 'attachment;
filename=' + 'output.pdf'
response.headers['Content-Type'] = contenttype.CONTENT_TYPE[
'.pdf']
return response.stream(file_path, chunk_size=10**6, filename=
'output.pdf')
client code:
import requests
import json
payload = """
...
<chart_params_in_json>
...
"""
data_dict = json.loads(payload)
url = 'http://127.0.0.1:8000/ChartGenerator/charts/generate'
resp = requests.post(url, data=json.dumps(data_dict), stream=True)
filename = 'x.pdf'
with open(filename, 'wb') as f:
for chunk in resp.iter_content(chunk_size=10**6):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
print resp.headers
{
'content-length': '15',
'set-cookie':
'session_id_chartgenerator=127.0.0.1-e21b530d-b5e0-4a6f-aa41-63e394124d5c;Path=/'
,
'server': 'Rocket1.2.6Python/2.7.5',
'connection': 'keep-alive',
'date': 'Wed,
20Aug201413: 22: 47GMT',
'content-type': 'text/html;charset=UTF-8'
}
The downloaded file via
request.post
has only 1KB, but the original file has 145KB. Content-Type on server side
does not get set to 'application/pdf', even though I explicitly set it with
following line:
response.headers['Content-Type'] = contenttype.CONTENT_TYPE['.pdf']
I guess this is the problem, because when I tried to open the downloaded
pdf file an error appeared saying that the file cannot be opened due to
invalid encoding method.
With the original file everything is alright, because it is generated
properly after receiving chart params from the client.
How can I solve this?
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.