dowload a stream file

114 views
Skip to first unread message

Florian Thonier

unread,
Oct 9, 2023, 10:17:01 AM10/9/23
to py4web
Hi everyone,
I convert my backend server from web2py to py4web and I need your help on one point that don't seem clear to me.

I need to download a local json file. But before that, I need to make some modifications in it. So I open it, make modifiations, and return a stream of the json dump.
Before, my function was like that:

```
dumped_json = json.dumps(data, separators=(',',':'))
return response.stream(StringIO.StringIO(dumped_json), attachment = True, filename = request.query['filename'])
```
But this don't work with py4web.
I try to take a look on documentation and source code but I don't found a correct answer for my need.

Can you help me with this use case ?

Thanks,
Florian

clement chesnin

unread,
Oct 13, 2023, 6:27:13 AM10/13/23
to py4web
Hi,

I am currently dealing with a similar issue, any idea on how to do this ?
Thx

Clément.

Jim Steil

unread,
Oct 13, 2023, 9:44:56 AM10/13/23
to py4web
What errors are you seeing?

One thing I seem to recall is that StringIO isn't a thing in Python3.  It was replaced with io

Can anyone confirm or correct this?

-Jim

Florian Thonier

unread,
Oct 18, 2023, 8:07:42 AM10/18/23
to py4web
response.stream don't exist in web2py.
 I try:
```python
bottle.response.status = 200;
bottle.response.set_header('Content-type', "application/json")
bottle.response.body = dumped_json
return bottle.response
```
In this case, I got the file content printing in the browser, but no download of the file as previously.
I presume that I can change client side to send it as a download from browser, but do you see/know a way to keep previous behavior ?

Florian

CarlosDB

unread,
Oct 18, 2023, 11:04:17 AM10/18/23
to py4web
 This is what I usually do.

from py4web.core import response

response.headers['Content-Type'] = 'application/pdf'  # Removed to force file download
response.headers['Content-Disposition'] = f'attachment; filename=myfile.json'
return dumped_json

Carlos.

Florian Thonier

unread,
Oct 19, 2023, 8:09:49 AM10/19/23
to py4web
Perfect. That works.

Thank you.
Reply all
Reply to author
Forward
0 new messages