[Zope] python Script to download a file

Casey Duncan casey at zope.com
Thu Jan 15 14:47:16 EST 2004


On Thu, 15 Jan 2004 12:09:14 -0600
"J. Cameron Cooper" <jccooper at jcameroncooper.com> wrote:

> Hervé Richard wrote:
> 
> > I write a python script which return a selected file to download.
> > The script receive value from a form to choose the kind of file to 
> > download (and increment the counter of the file selected):
> > For example, in a folder i have 2 files ("A.tgz" and "B.tgz") and a 
> > Page Template Object (chooseFileToDownload.html). This page is a
> > form with multiple choice in order to seclect a file to download (if
> > the user select "A" button, he must get the A.tgz file, if select
> > "B" he must get A.tgz ...)
> >
> > I write the python script method to select the file but i don't know
> > 
> > how the script sends the file to download:
> > With the variable "fileToDownload" which contain the name of the
> > file selected, if i write "return fileToDownload" in my script i get
> > all the file (in his compressed format) in my navigator windows, but
> > *no* a popup windows which ask me where i want to save the file like
> > we have any time we want to download a file.
> 
> There are many a way to get an object based on a string. See 
> zopelabs.com for some examples. One of the most common is::
> 
> name = "some_string"
> obj = context[name]
> 
> What you want to do with 'obj' depends on what you really want. If you
> 
> intend to write the contents of the object, you can::
> 
> return obj.index_html()
> # or
> return obj.data

The above will only work if the file is less than the file chunk size
(64K). Otherwise the former will not actually return anything (instead
it will write directly to the RESPONSE), and the latter will only return
the first 64K of the data.

The canonical way to get the data from a file object is (drum roll
please...):

str(obj)

This will work regardless of the size of the object. Granted it will
create an in-memory string that is the same size as the file data. So be
careful doing this if the file data is large.

-Casey



More information about the Zope mailing list