can't find the exact header for sending ods file as a downloadable file

149 views
Skip to first unread message

Krishnakant Mane

unread,
Dec 21, 2012, 2:49:37 PM12/21/12
to pylons-...@googlegroups.com
Hello all.
I wish to send across an ods (spreadsheet) file from my pylons
controller in such a way that the browser should ask the user to
download the file (by presenting the usual download dialog of course ).
Can some one help here?
happy hacking.
Krishnakant.

Jason

unread,
Dec 21, 2012, 3:40:44 PM12/21/12
to pylons-...@googlegroups.com
Once you have the ods as a file object in your view-callable you can return it with something like:

filecontent = myfile.read()
headers = {
    'Content-Type': 'application/ods',
    'Content-Length': len(filecontent), 
    'Content-Disposition': 'attachment; filename=myfilename.ods' }
return Response(filecontent, headerlist=headers.items()) 

You will have to find out what the correct content-type is for ODS files though. 

Krishnakant Mane

unread,
Dec 23, 2012, 1:42:57 PM12/23/12
to pylons-...@googlegroups.com, Jason
On 12/22/2012 02:10 AM, Jason wrote:
hi Jason,
Thanks for the idea.


Once you have the ods as a file object in your view-callable you can return it with something like:

filecontent = myfile.read()
headers = {
    'Content-Type': 'application/ods',
    'Content-Length': len(filecontent), 
    'Content-Disposition': 'attachment; filename=myfilename.ods' }
return Response(filecontent, headerlist=headers.items()) 



I tried this.

curdir = os.getcwd()
            ledgerfile = open(curdir + "/Ledger.ods","r")
            ledgerdata = ledgerfile.read()
            headers = {'Content-Type':'application/vnd.oasis.opendocument.spreadsheet' ,'Content-disposition': 'attachment; filename=' + curdir  + '/Ledger.ods', 'Content-Length': len(ledgerdata)}

            ledgerfile.close()

            return Response(ledgerdata, headerlist= headers.items())

Note that this is the Response object resulting from
from pylons.controllers.util import abort, redirect, Response
So it is not the response (with a small r ) as in from pylons import request response, although this import is also there in my code.
Now what happens is that my browser (Firefox) displays the content of spreadsheet in a junk form instead of showing me the dialog box to download the file.
Can any one suggest what could be the problem?
Happy hacking.
Krishnakant.

Jason

unread,
Dec 26, 2012, 10:30:47 AM12/26/12
to pylons-...@googlegroups.com


On Friday, December 21, 2012 2:49:37 PM UTC-5, kk.gnu wrote:
Looks like I gave you the Pyramid version. Here is what I use for Pylons:


response.headers['Content-type'] = 'application/pdf'
response.headers['Content-disposition'] = 'attachment; filename={0}'.format(filename)
response.headers['Content-length'] = len(filecontent)
return filecontent 
Reply all
Reply to author
Forward
0 new messages