I am trying to learn how to download text/csv files.

This test function works fine.

def download():
        # import and assign handle
    import cStringIO
    content = cStringIO.StringIO()

        # write dummy content
    for i in range(0, 10):
        content.write("This is the text"+"\r\n")

        # assign filename to download file
    filename = 'outputfile.txt'

    # send content for download
    response.headers['Content-Disposition'] = 'attachment; filename='
+ filename
    response.headers['Content-Type'] = 'text/csv'
    return content.getvalue()


However, when I extract the section that helps to download the file
into a generalized function 'do_download', it does not work. What can
be done to get it to work?

def download():
    import cStringIO
    content = cStringIO.StringIO()
    for i in range(0, 1000):
        content.write("This is the text"+"\r\n")
    filename = 'outputfile.txt'
    do_download(filename, content)

def do_download(filename, content):
    response.headers['Content-Disposition'] = 'attachment; filename='
+ filename
    response.headers['Content-Type'] = 'text/csv'
    return content.getvalue()

Reply via email to