[Zope] python Script to download a file

J. Cameron Cooper jccooper at jcameroncooper.com
Thu Jan 15 13:09:14 EST 2004


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

You can redirect to that object like::

context.REQUEST.RESPONSE.redirect(obj.absolute_url())

The file download dialogue is a function of the browser, and it will 
always do this for MIME types it recognizes as non-displayable. I think 
the first method may fool it sufficiently that it displays the (mostly 
nonsense) contents of an archive file in the browser.

          --jcc

-- 
"My point and period will be throughly wrought,
Or well or ill, as this day's battle's fought."





More information about the Zope mailing list