Найти - Пользователи
Полная версия: Как вернуть content_type="text/csv", но с именем файла?
Начало » Pyramid / Pylons / TurboGears » Как вернуть content_type="text/csv", но с именем файла?
1
s_vilgelm
В общем есть код:
    @expose(content_type="text/csv")
def get_context_csv(self, start_month, start_year, tariff_id, start_hour, start_day, start_minute):
outputstream=StringIO.StringIO()
writer=csv.writer(outputstream)
...
return outputstream.getvalue()
При вызове открывается диалог сохранение файла, по умолчанию имя файла get_context_csv, как его можно задать?
zheromo
from tg import request, response
from tg.controllers import CUSTOM_CONTENT_TYPE

class MyController(BaseController):
@expose(content_type=CUSTOM_CONTENT_TYPE)
def stats(self):
response.content_type = 'text/csv'
response.headerlist.append(('Content-Disposition','attachment;filename=stats.csv'))
return '1,2,3'
s_vilgelm
О, спасибо большое.
s_vilgelm
Для версии 1.0 решение такое:
from cherrypy import request, response

class MyController(BaseController):
@expose(content_type='text/csv')
def stats(self):
response.headers['Content-Disposition'] = 'attachment;filename=stats.csv'
return '1,2,3'
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB