On Oct 10, 2006, at 10:39 PM, Muhammad wrote:

>
> Hi
>
> I am using the following code to download a file (Which is not on
> public folders.)
>
> import os
> dir = "dirpath"
>
> def download(self):
>         f = request.params['file']
>         fpath = dir + '/' + f
>         if os.path.isfile(fpath):
>                 fhandle = open(fpath, 'r')
>                 file    = fhandle.read()
>                 fhandle.close()
>
>                 R = Response()
>                 R.headers['Content-Type'] = "application/octet-stream;
> name=%s" % (f)
>                 R.headers['Content-Type'] =
> "application/force-download"
>                 R.headers['Content-Transfer-Encoding'] = "Binary"
>                 R.headers['Content-length'] = "%s" % (len(file))
>                 R.headers['Content-Disposition'] = "attachment;
> filename= %s" %(f)
>                 #print file
>                 return R
>
>
> After running this code the browser will download the file, but the
> size of file is zero bytes.
>
> As I know in PHP if you will run readfile($filedir) after headers, it
> will download the file.
>
> so, 1) Is There any other way to do in python?

Paste provides a WSGI application (paste.fileapp.FileApp) for serving  
file downloads. Take a look at your pylons project's default error  
controller for an example of how to use it, in the img/style actions.

What you have here appears to be close to providing the actual file  
download -- you're just missing the actual file content (as  
R.content). However paste's FileApp will provide an easier, and more  
efficient implementation (in terms of how it handles streaming the  
data to the client and the support of last modified/etag headers), as  
well as being well tested.


--
Philip Jenvey


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss
-~----------~----~----~----~------~----~------~--~---

Reply via email to