[Product-Developers] Generating file downloads on the fly

Hedley Roos hedley at upfrontsystems.co.za
Fri May 15 09:09:00 UTC 2009


Rowan Woodhouse wrote:
> Hi,
> 
> I need to generate a download file on the fly (it's an excel spread sheet
> with content drawn from a database) but I'm having some difficulty working
> out the best way to do this.
> 
> At the moment I've got an Archetype based product that has a FileField in
> the schema. So far I've been attempting to work out a way to create the
> correct sort of accessor so that the generated file is never saved in Zope
> itself (at the moment I just get the file coming over as plain text mime
> type). Is this the best approach? If so could someone point me in the
> correct direction on how to wrap the file correctly so that it downloads
> correctly instead of coming over as a plain text file? If not could someone
> point me in a more sensible direction?
> 
> Regards,
> Rowan


Create a method on your class or a browser view. The latter is 
preferred. Regardless which you choose you will have a method to do the 
work somewhere, eg.

def download(self, data):
     """
     Make data available as download.
     data is a string representation of the Excel data.
     This method is an example and not concerned with encoding issues.
     """

     # Set headers and return
     setheader = self.REQUEST.RESPONSE.setHeader
     setheader('Content-Length', len(data))
     setheader('Content-Type', 'application/x-msexcel; charset=utf-8')
     setheader('Content-Disposition', 'filename=myfile.xls')
     return data

Not tested!

Hedley




More information about the Product-Developers mailing list