0

シリアル化を返すというアクションを持つPylonsコントローラーがありcontent_type=text/csvます。入力パターンに基づいてアクションの応答に名前を付けたいと思います。つまり、次のルートの場合、生成されたcsvファイルには{id}.csv/app/PROD/serialize=>という名前を付ける必要がありPROD.csvます(ユーザーがExcelでファイルを適切な名前で直接開くことができるようにウェブブラウザ)

map.connect('/app/{id}/serialize',controller = 'csvproducer',action='serialize')

運が悪かったので、webobの応答オブジェクトのさまざまなHTTPヘッダーとプロパティを設定しようとしました。ただし、コントローラーに新しいアクションを追加し、元のアクションをその新しいアクションに動的にリダイレクトするだけで、回避策を見つけました。

map.connect('/app/{id}/serialize',controller = 'csvproducer',action='serialize')
map.connect('/app/csv/{foo}',controller = 'csvproducer', action='tocsv')

コントローラのスニペット:

def serialize(self,id):
  try:
    session['key'] = self.service.serialize(id) #produces csv content
    session.save()
    redirect_to(str("/app/csv/%s.csv" % id))
  except Exception,e:
    log.error(e)
    abort(503)

def tocsv(self):
  try:
    csv = session.pop("rfa.enviornment.serialize")
  except Exception,e:
    log.error(e)
    abort(503)
  if csv:
    response.content_type='text/csv'
    response.status_int=200
    response.write(csv)
  else:
    abort(404)

The above setup works perfectly fine, however, is there a better/slicker/neater way of doing it? Ideally I wouldn't like to redirect the request; instead I'd like to either rename location or set content-disposition: attachment; filename='XXX.csv' [ unsuccessfully tried both :( ]

Am I missing something obvious here?

Cheers

UPDATE: Thanks to ebo I've managed to do fix content-disposition. Should better read W3C specs next time ;)

4

1 に答える 1

2

You should be able to set the content-disposition header on a response object.

If you have already tried that, it may not have worked because the http standard says that the quotes should be done by double-quote marks.

于 2010-02-17T23:48:24.657 に答える