[Zope3-Users] dynamic file creation

Frank Burkhardt fbo2 at gmx.net
Fri Apr 14 11:42:50 EDT 2006


Hi,

On Fri, Apr 14, 2006 at 02:56:55PM +0200, Michal Nowikowski wrote:
> Hello,
> 
> I'm a Zope3 newbie. I want to send to user a file while he clicks on a link.
> The file is created dynamically and sent to user for example in ZIP format.
> I think I had to use IFile interface and File class. But I dont know how to
> prepare it and send it to user. My code:
> 
> class ISomeContainer(IReadContainer):
>     contains(IFile)
> 
> then I have:
> 
> self['zip'] = File("some data", "application/zip")
> 
> So when I access url: http://somepath/zip
> browser shows 'some data', but I want to see 'Save As' dialog window.
> How to do that?

You won't need the IFile interface - just a simple view that sets some special
HTTP-headers. Example:

class DownloadView(BrowserView):
	def __call__(self):
		rsp=self.request.response
		rsp.setHeader('Content-disposition','attachment; filename=download.txt')
		rsp.setHeader('Content-Type','text/plain')
		return "This is a textfile"

The type of data you return, the default filename the user gets presentend in
his download dialog ('download.txt' in the example) and the data itself
is 100% under your control. There should be no problem creating some kind of
archive inside the view and return in to the user.

Remember: You must always return a plain "str"ing from __call__ if you want
to manually set the 'Content-Type' -header.

Regards,

Frank


More information about the Zope3-users mailing list