Downloading a csv file in django

django, download

Solution

Thanks everyone for your suggestions. I picked a few new tricks :) However I think I have found the answer to my problem here: Downloading CSV via AJAX My "savefile" function is called via an Ajax request and it appears that ajax has a limitation where"save as dialog box" does not appear no matter what the HTTP headers are.

I should have mentioned that I use Ajax to call this function but it never occurred to me that this could be an issue.:) Thankyou StackOverflow!

Problem

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!

Original source

Related problems