On Wed, Apr 14, 2010 at 2:44 AM, Krishnakant Mane <[email protected]> wrote:
>> By the way, if the PDF is already in a file with a .pdf extension, you
>> can use paste.fileapp to send it, which saves some work.
>>
> Thanks a lot!  the files are indeed getting saved as .pdf so I guess the
> fileapp will directly send out the file to the browser.
> So do I use the paist.fileapp instead of return?
> and to use fileapp do I still need to set the response header (content type
> ) or will that function handle the work?

from paste.fileapp import FileApp
from pylons.controllers.util import forward
class MyController(BaseController):
    def my_action(self):
        path = "/mypdfs/a.pdf"
        app = FileApp(path)
        return forward(app)

It will guess the MIME type from the extension. You can pass keyword
args or subclass it to override the MIME type or set a caching policy.

There's also DirectoryApp in the same module if you want to serve a
hierarchy of static files. Use the 'path_info' routing variable for
this, which magically sets PATH_INFO in the WSGI environment:

    map.connect("/prefix/{path_info:.*", controller="mycontroller",
action="myaction")
    # Matches "/prefix/subdir/subdir2/a.pdf" => /mypdfs/subdir/subdir2/a.pdf

I think FileApp raises an exception if an intermediate directory
doesn't exist, which causes a 500 error rather than the desired 404.
So I capture that case and abort:

    if not os.path.exists(path):
        abort(404)
    app = FileApp(path)
    return forward(app)

-- 
Mike Orr <[email protected]>

-- 
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?hl=en.

Reply via email to