TG2 downlaoding a file

7 views
Skip to first unread message

adam

unread,
Nov 9, 2009, 6:01:14 AM11/9/09
to TurboGears
I have my tg2 app and i want a user to be able to download an image.
They click on download, decide where to save the file and then it
downloads. Is there a simple tg2 app for this?

Thanks for any help

Adam

Diez B. Roggisch

unread,
Nov 9, 2009, 7:58:42 AM11/9/09
to turbo...@googlegroups.com
On Monday 09 November 2009 12:01:14 adam wrote:
> I have my tg2 app and i want a user to be able to download an image.
> They click on download, decide where to save the file and then it
> downloads. Is there a simple tg2 app for this?

This should get you started:

http://turbogears.org/2.0/docs/main/ResponseTypes.html

Diez

adam

unread,
Nov 10, 2009, 6:23:45 AM11/10/09
to TurboGears
ok so i have

@expose(content_type=CUSTOM_CONTENT_TYPE)
def Download(self,file):
username = request.identity['repoze.who.userid']
dir_name = session.get('dir', None)
filename = '/home/rubberduckiee/webapps/editor/Xinha/
plugins/ExtendedFileManager/files'+str(dir_name)+'/'+str(file)

st = os.stat(filename)
size = st[ST_SIZE]

rh = response.headers
rh['Pragma'] = 'public' # for IE
rh['Expires'] = '0'
rh['Cache-control'] = 'must-revalidate, post-check=0, pre-
check=0' #for IE
rh['Cache-control'] = 'max-age=0' #for IE
rh['Content-Type'] = 'image/jpg'
disposition = 'attachment; filename="'+filename+'"'
rh['Content-disposition'] = disposition
rh['Content-Transfer-Encoding'] = 'binary'
rh['Content-Length'] = size


a file downloads but the it fails to open because the file is empty.
What am i missing

Do i need to read the file to an output? if so how?

Diez B. Roggisch

unread,
Nov 10, 2009, 7:05:17 AM11/10/09
to turbo...@googlegroups.com
adam schrieb:

Returning the actual file contents? How else should that happen?

And you should get rid of the str() - instead, use the encoding your
filesystem uses, and do something like


fname = "/path/%s/%s" % (dir_name.encode("utf-8"), file.encode("utf-8"))


Diez

Reply all
Reply to author
Forward
0 new messages