Displaying Uploaded PDF Documents

611 views
Skip to first unread message

azarkowsky

unread,
Dec 14, 2010, 10:50:11 AM12/14/10
to web2py-users
In my web2py application I'm allowing users to upload PDF documents
(via an upload field) and then on another page I provide a download
link, however, I think because of the file renaming security feature
these PDF documents are truly being downloaded again (saved locally to
each user's desktop) instead of just displayed in a new browser window
as is the case with most other PDFs I encounter on other websites. Is
there any way to provide a link directly to a file that was uploaded
so the browser can display it without having to download the file
again?

Thanks,
Adam

DenesL

unread,
Dec 14, 2010, 11:45:02 AM12/14/10
to web2py-users

I believe this is more an option between 'save the file' or 'open in
the browser', but in either case the PDF file has to be sent/
downloaded to the client in order to be seen.
Otherwise the client would require direct access to the PDF file
location, as in shared resources over an internal network.

Bruno Rocha

unread,
Dec 14, 2010, 11:57:05 AM12/14/10
to web...@googlegroups.com
For security reasons, web2py does not expose the 'uploads' folder to the user, this folder can be accessed only by the 'download' function.

You can do that:

change the upload folder with 'uploadfolder' parameter.

<model>
db.define_table('pdfs',
                       Field('file','upload',uploadfolder=request.folder+'static/pdfs')
                       )
</model>

Create a viewer function wich redirects the user direct to the file, then, if the user has adobe acrobat plugin to view the file in browser, this file will be shown in the browser, else, will be downloaded.

<controller>
def viewer():
    row = db(db.pdfs.id==request.args(0)).select(db.pdfs.file).first()
    redirect(URL('static','pdfs',args=row.file))
</controller>

Now you can call this as:


which redirects the user to:



2010/12/14 azarkowsky <azark...@gmail.com>

mart

unread,
Dec 14, 2010, 2:29:18 PM12/14/10
to web2py-users
well... if a pdf needs to be viewed within a browser, there is always
flex. Although, Flex it self will not support PDF (because the reader
and the flash layer live separately), there are workarounds with
either flex-iframe (http://code.google.com/p/flex-iframe/) or
flexpaper (http://flexpaper.devaldi.com/) which seem to have done a
very good job where Adobe has clearly failed.

AIR is a good alternative if you don't mind having the pdf open in a
desktop version of flex. Here's an example of a very basic pdfViewer
with AIR.

if(HTMLLoader.pdfCapability == HTMLPDFCapability.STATUS_OK){
var htmlLoader:HTMLLoader = new HTMLLoader();
var url:URLRequest = new URLRequest(pathUrl); //URL to the file
htmlLoader.width = windowWidth;
htmlLoader.height = windowHeight;
htmlLoader.load(url);

//UIComponent
var pdfViewer:UIComponent = new UIComponent();
holder.addChild(htmlLoader);
addChild(pdfViewer);
}


Mart :)




On Dec 14, 11:57 am, Bruno Rocha <rochacbr...@gmail.com> wrote:
> For security reasons, web2py does not expose the 'uploads' folder to the
> user, this folder can be accessed only by the 'download' function.
>
> You can do that:
>
> change the upload folder with 'uploadfolder' parameter.
>
> <model>
> db.define_table('pdfs',
>
> Field('file','upload',uploadfolder=request.folder+'static/pdfs')
>                        )
> </model>
>
> Create a viewer function wich redirects the user direct to the file, then,
> if the user has adobe acrobat plugin to view the file in browser, this file
> will be shown in the browser, else, will be downloaded.
>
> <controller>
> def viewer():
>     row = db(db.pdfs.id==request.args(0)).select(db.pdfs.file).first()
>     redirect(URL('static','pdfs',args=row.file))
> </controller>
>
> Now you can call this as:
>
> http://127.0.0.1:8000/pdfs/default/viewer/3#record id
>
> which redirects the user to:http://127.0.0.1:8000/pdfs/static/pdfs/pdfs.file.aaf5d3f7779841d0.6e6...
>
> --
>
> Bruno Rochahttp://about.me/rochacbruno/bio
>
> 2010/12/14 azarkowsky <azarkow...@gmail.com>

azarkowsky

unread,
Dec 15, 2010, 1:27:58 PM12/15/10
to web2py-users
Thanks everybody for your suggestions! I went with what Bruno
suggested and it worked like a champ! I figured I wouldn't be able
to bypass the upload security mechanism, but the uploadfolder
parameter was the key piece I needed to get the desired behavior.

On Dec 14, 11:57 am, Bruno Rocha <rochacbr...@gmail.com> wrote:
> For security reasons, web2py does not expose the 'uploads' folder to the
> user, this folder can be accessed only by the 'download' function.
>
> You can do that:
>
> change the upload folder with 'uploadfolder' parameter.
>
> <model>
> db.define_table('pdfs',
>
> Field('file','upload',uploadfolder=request.folder+'static/pdfs')
>                        )
> </model>
>
> Create a viewer function wich redirects the user direct to the file, then,
> if the user has adobe acrobat plugin to view the file in browser, this file
> will be shown in the browser, else, will be downloaded.
>
> <controller>
> def viewer():
>     row = db(db.pdfs.id==request.args(0)).select(db.pdfs.file).first()
>     redirect(URL('static','pdfs',args=row.file))
> </controller>
>
> Now you can call this as:
>
> http://127.0.0.1:8000/pdfs/default/viewer/3#record id
>
> which redirects the user to:http://127.0.0.1:8000/pdfs/static/pdfs/pdfs.file.aaf5d3f7779841d0.6e6...
>
> --
>
> Bruno Rochahttp://about.me/rochacbruno/bio
>
> 2010/12/14 azarkowsky <azarkow...@gmail.com>
>
>
>
>
>
>
>

Michael Dunga

unread,
May 1, 2018, 3:14:11 PM5/1/18
to web2py-users
thank you. I needed it too

mostwanted

unread,
Mar 1, 2019, 7:40:47 AM3/1/19
to web2py-users
I am attempting Bruno's above code to display the saved PDF, i have created a link in my index page that takes me to the viewer page but when i click it i get an error message below:

<type 'exceptions.AttributeError'> 'NoneType' object has no attribute 'fileS'

 MODEL CODE:

db.define_table('pdfs',
                Field('fileS','upload',uploadfolder=request.folder+'static/pdfs'))


CONTROLLER CODE
def viewer():
    row = db(db.pdfDocs.id==request.args(0)).select(db.pdfDocs.fileS).first()
    redirect(URL('static','pdfs',args=row.fileS))

 
VIEW CODE (index)
{{extend 'layout.html'}}

{{=A('Click to view', _href=URL('default', 'viewer'))}}

I changed the 'file' variable because it said its an  "ALL" reserved SQL/NOSQL keyword
What can i do to make this link redirect me without an error i'm getting??

Regards;

Mostwanted

Christian Varas

unread,
Mar 1, 2019, 7:51:18 AM3/1/19
to web...@googlegroups.com
Your model says:
db.define_table('pdfs')

And you controller points to another table (db.pdfDocs)

Your controller should be 

CONTROLLER CODE

def viewer():
    row = db(db.pdfs.id==request.args(0)).select(db.pdfs.fileS).first()

    redirect(URL('static','pdfs',args=row.fileS))

 
Cheers.
Chris.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mostwanted

unread,
Mar 3, 2019, 2:55:41 AM3/3/19
to web...@googlegroups.com
I apologize for that oversight, i fixed my controller:
def viewer():

    row
= db(db.pdfs.id==request.args(0)).select(db.pdfs.fileS).first()
    redirect
(URL('static','pdfs',args=row.fileS))

I figured out my mistake, i didnt have a function that called & unpacked the database details so my link would have an args parameter:
{{=A('Click to view', _href=URL('viewer', args=details.id))}}<br />
Thats why i had a NoneType error, thats because nothing was selected!

Thanks Chris V.

Mostwanted
Reply all
Reply to author
Forward
0 new messages