Como mostrar a imagem no HTML com web2py

59 views
Skip to first unread message

Kimus

unread,
Aug 6, 2019, 11:21:57 AM8/6/19
to web2py-users
olha eu aqui dnv  :) 

Estou com um problema eu insiro uma imagem no banco mas na hr de mostrar no HTML não consigo de jeito nem um, eu nãoi to  usando o template do web2py então td pra mim é mais dificil alguem teria algum jeito de fazer 

OBS : eu coloquei um campo extra_field no auth_user como foto e queria exibir essa foto, só que não utilizo quase nd do que o web2py tem pronto tipo sqlform grid essas coisas dai tenho que fazer mais manual a coisa alguem teria como me dar uma luz 

To tentando algo do genero  tentei usar a função download que ta na default só que no meu controller só que ainda não funciona quando salvo a imagem ela upa a imagem no upload  tipo auth_user.foto e uma hash só qe não consigo fazer funcionar msm alguem me ajuda :( 

<img src="{{=URL(c='template',f='download',args='auth_user.foto.file')}}">

Dave S

unread,
Aug 6, 2019, 5:27:56 PM8/6/19
to web2py-users


An upload field will store the original filename and an obfuscated filename (that is, a new name for the file that contains a unique portion and an encoding of the original name).  The obfuscated filename, which looks rather like a hash, is the filename used by the filesystem.   The user/download function in the controllers/default.py understands how to handle this.  The URL to download an uploaded file is generated by

A(" click to download", _href= URL("download", args=row.fdata)
and looks like
download/uploadf.fdata.81a339e105991c67.454e482d3570317670303956505530653369757a394d73484a64465f70657270657475616c5f5352562d48565f323031393038303631373237.txt


Translating this to display an image, here's a controller function:

def testimg():
   row
= db(db.uploadf.fname=="test1").select().first()
   
return dict(img = row.fdata)

using the model

db.define_table('uploadf',
               
Field('fname', 'string'),
               
Field('fdata', 'upload'),migrate='uploadf.table')

and the view testimg.html looks like

{{extend 'layout.html'}}
{{block header}}
Blah!
{{end}}
{{=IMG(_src=URL("download", img), _alt="test image")}}

After the page is loaded, if you use the browser tools to Inspect Element, you're shown

<img alt="test image" src="/updater/download/uploadf.fdata.adbba9e59b5e3383.323031392d776f6d656e732d776f726c642d6375702d6461792d32302d353739383430333339333435343038302d6c61772e676966.gif">

To connect with your auth_user table, you may want to add an extra field that references the file uploaded image.

For more on upload fields, see
<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Field-types>
<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#More-on-uploads>
and
<URL:http://web2py.com/books/default/chapter/29/09/access-control#Customizing-Auth>

or
<URL:http://web2py.com/books/default/chapter/31/06/a-camada-de-abstracao-do-banco-de-dados#-Tipos-de-campo>
<URL:http://web2py.com/books/default/chapter/31/06/a-camada-de-abstracao-do-banco-de-dados#-Mais-sobre-uploads>
and
<URL:http://web2py.com/books/default/chapter/31/09/controle-de-acesso#-Personalizando-Auth>

for the Portuguese (Spanish is "41" instead of "31" or "29").

Good luck!

Dave S
/dps


Kimus

unread,
Aug 7, 2019, 2:05:14 PM8/7/19
to web2py-users
ola eu segui essas instruções e deu quase td certo, só que a imagem não aparece de jeito nem um

<div class="logo">
     {{=IMG(_src=URL("download", auth.user.foto))}}

     </div>

eu não criei a função pq eu não preciso ja que o auth_user ja esta na pagina que desejo carregar a foto


poderia me dar mais uma luz ??

Dave S

unread,
Aug 7, 2019, 5:47:36 PM8/7/19
to web...@googlegroups.com


On Wednesday, August 7, 2019 at 11:05:14 AM UTC-7, Kimus wrote:
ola eu segui essas instruções e deu quase td certo, só que a imagem não aparece de jeito nem um

<div class="logo">
     {{=IMG(_src=URL("download", auth.user.foto))}}

     </div>

eu não criei a função pq eu não preciso ja que o auth_user ja esta na pagina que desejo carregar a foto


poderia me dar mais uma luz ??


I don't think the "download" tool works with images (or whatever) stored in auth.user [1].  It is designed to be used with upload fields, and expects the data location to be found through the upload field of whatever table you're using for uploads.  If you really want the picture stored in auth.user, you are going to have to (AIUI) stream it yourself, but I would recommend instead having a reference in auth.user to the correct entry in the table you use for uploads, and to let download handle it with the boot-in tools.

If you do want to try streaming, I've sketched it a little in
<URL:https://groups.google.com/d/msg/web2py/MTJoZc1Kh6I/oYjxBn2KCQAJ>
and the archives should have more (I had to learn it from somewhere myself ). Also see the "More on uploads" section in the Web2py book links.

[1]  You can use your browser's "Inspect Element" tool to see what the image link turned out as.

/dps



Marco Mansilla

unread,
Aug 9, 2019, 12:47:21 PM8/9/19
to web...@googlegroups.com
Actually the download method is written in the default controller,  unless you have written your own function in template. 

    {{=IMG(_src=URL('default','download', auth_user.foto))}}

Anyway, could you provide the auth_user table definition?, did you add extra fields?, Since auth_user.foto.file should return an error, auth_user.foto should be an upload field to return the file name, if it is a blob field you should put the result in src with no url.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/9131340d-6d7a-4d8f-bfa1-3fc0b0001ba7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages