IE problems when response headers are changed inside the controller

43 views
Skip to first unread message

León Domingo

unread,
Oct 11, 2012, 7:51:41 AM10/11/12
to turbo...@googlegroups.com
Hi people,

I have this controller method:

        @expose()
    def get(self, codigo):
       
        aa = dbs.query(ArchivosAsociados).\
            filter(ArchivosAsociados.codigo == codigo).first()
           
        if aa.caducidad and aa.caducidad > dt.datetime.now():
            content, mt, file_name = SapnsDoc.download(aa.id_recurso)
            response.headerlist.append(('Content-Type', mt.encode('utf-8')))
            response.headerlist.append(('Content-Disposition', 'attachment;filename=%s' % file_name))
           
            return content
           
        else:
            redirect(url('/banco_de_recursos/recurso_caducado/', params=dict(codigo=codigo)))


As you can see "mime-type" is "calculated" ("Content-Type" header is added dynamically) inside the method. I've read a record from the database which gives me the "mime-type" (mt) and "file name" (file_name). Content (content) is read from the file itself which is stored in the hard disk.

Ok, this thing does not work on IE8 or IE7. Chrome, FF, Opera, Safari or even IE9 do the right thing.

Any suggestion?

Thanks in advance

León

Alessandro Molina

unread,
Oct 11, 2012, 6:12:22 PM10/11/12
to turbo...@googlegroups.com
mt.encode('utf-8') might end up being wrong, HTTP Headers should only
be latin1 if I remember correctly.
Even though, probably, there aren't mime types which contain utf8 characters.

Also you should be able to use response.content_type =
'yourcontenttype' and response.headers['Content-Disposition']=...
instead of relying on response.headerlist.append, which is a bit
easier to read and write.
> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/EIFdY1yODPgJ.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to
> turbogears+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.

León Domingo

unread,
Nov 1, 2012, 5:59:51 PM11/1/12
to turbo...@googlegroups.com
Hi again,
I tested it and I think it works.

The reason to encode (.encode('utf-8')) is that those mime types come from a database and SQLAlchemy give them to me as unicode, so I have to encode them. Now I do the same thing but using "latin1" coding instead of "utf-8".

Thanks, Alessandro

León Domingo

Ender - Factoría de Software
leon.d...@ender.es
Avda. Cerro del Águila, 7. S23 - 2ª Planta
28703 S.S. de los Reyes (MADRID)
Tlf. 902 01 44 01

   
Reply all
Reply to author
Forward
0 new messages