Downloading a csv file in django

Posted by spyder on Stack Overflow See other posts from Stack Overflow or by spyder
Published on 2010-03-17T18:33:48Z Indexed on 2010/03/17 18:51 UTC
Read the original article Hit count: 223

Filed under:
|

I am trying to download a CSV file using HttpResponse to make sure that the browser treats it as an attachment. I follow the instructions provided here but my browser does not prompt a "Save As" dialog. I cannot figure out what is wrong with my function. All help is appreciated.

  dev savefile(request):
        try:
            myfile = request.GET['filename']
            filepath = settings.MEDIA_ROOT + 'results/'
            destpath = os.path.join(filepath, myfile)
            response = HttpResponse(FileWrapper(file(destpath)), mimetype='text/csv' ) 
            response['Content-Disposition'] = 'attachment; filename="%s"' %(myfile)
            return response
        except Exception, err:
            errmsg = "%s"%(err)
            return HttpResponse(errmsg)

Happy Pat's day!

© Stack Overflow or respective owner

Related posts about django

Related posts about file-download