How to make a function to download a file?

2,574 views
Skip to first unread message

thstart

unread,
Dec 22, 2011, 10:09:04 PM12/22/11
to web...@googlegroups.com
I want the visitors to be able to download a CSV file.

here is my code:
def download():
        response.headers['Content-Type'] = 'text/csv'
        attachment = 'attachment;filename=' + file+ '.csv'
        response.headers['Content-Disposition'] = attachment
        
        content = '....,....,....'
        raise HTTP(200,str(content),
                   **{'Content-Type':'text/csv',
                      'Content-Disposition':attachment + ';'})

When I click on the link download, I get 'Undefined' in the browser
and nothing is downloaded.

What is wrong?

DenesL

unread,
Dec 23, 2011, 11:28:04 AM12/23/11
to web2py-users

Look at the code samples under
http://web2py.com/books/default/chapter/29/10#CSV

Rahul

unread,
Dec 27, 2011, 1:14:22 AM12/27/11
to web2py-users
I tried to achieve something similar with below link - have a look.
Although I did not change the downloads() default function. It worked
for me..
http://groups.google.com/group/flutuberwachung/browse_thread/thread/32b9daae6cc14840/e03404cf2d6e35b5#e03404cf2d6e35b5

Cheers Rahul

On Dec 23, 9:28 pm, DenesL <denes1...@yahoo.ca> wrote:
> Look at the code samples underhttp://web2py.com/books/default/chapter/29/10#CSV

thstart

unread,
Dec 27, 2011, 2:47:54 PM12/27/11
to web...@googlegroups.com
I don't use a database to create the CSV content but generate is as a comma delimited text. Also I want to set the file name to be downloaded.

I don't see how to do it from the book.

This line has to be changed in order to work:

Anthony

unread,
Dec 27, 2011, 4:18:04 PM12/27/11
to web...@googlegroups.com
Maybe try writing the content to a StringIO object and using response.stream (see http://web2py.com/books/default/chapter/29/4#response).

Anthony

thstart

unread,
Dec 28, 2011, 1:05:43 PM12/28/11
to web...@googlegroups.com
As I see I need this:
 stream = cStringIO.StringIO()
return stream.getvalue()

I have a ready to go content as a string in a variable:
content =  '.....'

So how to put this string in a stream?

My code is:

thstart

unread,
Dec 28, 2011, 1:13:03 PM12/28/11
to web...@googlegroups.com
a note I need to use Python 2.5. On GAE Python 2.7 is extremely slow.

Anthony

unread,
Dec 28, 2011, 2:39:56 PM12/28/11
to web...@googlegroups.com
On Wednesday, December 28, 2011 1:05:43 PM UTC-5, thstart wrote:
As I see I need this:
 stream = cStringIO.StringIO()
return stream.getvalue()

Actually, that will do the same thing you've already tried (stream.getvalue() is just a string, so you'll be returning a string, just like your original code). In fact, when I try your original code, it works fine, so perhaps your problem is with the link or routing (i.e., the link isn't properly routing to the download function). Also, in your code, where does the 'file' variable come from?

Anthony

thstart

unread,
Dec 28, 2011, 3:55:12 PM12/28/11
to web...@googlegroups.com
file is composed dynamically - I get it from request - it is send like ?file=name and I get it from there.

can you post the code you just tried?

Anthony

unread,
Dec 28, 2011, 4:07:23 PM12/28/11
to web...@googlegroups.com
I literally just copied and pasted your code into a basic 'welcome' app, except I hard-coded a filename ("data.csv"):

def download():
        response.headers['Content-Type'] = 'text/csv'
        attachment = 'attachment;filename=data.csv'
        response.headers['Content-Disposition'] = attachment
        content = '....,....,....'
        raise HTTP(200,str(content),
                   **{'Content-Type':'text/csv',
                      'Content-Disposition':attachment + ';'})

Then I entered that URL in my browser address bar, and I got a CSV file download, which opened right up in Excel (it included one row of data, with "..." in each of the three first columns).

Anthony

thstart

unread,
Dec 28, 2011, 4:10:55 PM12/28/11
to web...@googlegroups.com
You mean my original code is working? With IE it is not.
What browser you are using? In IE nothing comes
for download.
Message has been deleted

Anthony

unread,
Dec 28, 2011, 5:56:01 PM12/28/11
to web...@googlegroups.com
Works in IE7 and IE9 for me. Are you clicking a link or going directly to /appname/controller/download in the browser? Perhaps the link is incorrect or there's a problem with the filename (though not sure that would affect the download). Also, note that the default.py controller of the 'welcome' app already includes a download() function -- if you've added a second download function to that controller, I think that will cause a problem.

Anthony 

Constantine Vasil

unread,
Dec 28, 2011, 5:58:03 PM12/28/11
to web...@googlegroups.com
I fired the debugger, when I click on the link,
the debugger stops at the function (the name is export),

it comes down to  raise HTTP but does not downloads
the file.

Anthony

unread,
Dec 28, 2011, 6:18:31 PM12/28/11
to web...@googlegroups.com
Not sure what the problem is. What version of web2py? Can you attach a minimal app that reproduces the problem?
Reply all
Reply to author
Forward
0 new messages