Discussion:
[web2py] How to use uploaded image
michael sun
2015-07-25 01:45:18 UTC
Permalink
I build a table:

db.define_table('image',
Field('title', unique=True),
Field('file', 'upload'),
format = '%(title)s')

then upload a image called 'water'.

in index.html, I wrote:
<img src="{{=URL('download',args=db.image.water)}}" />

but then it gives me the error:

<type 'exceptions.AttributeError'>()
Function argument list

(self=<Table image (id,title,file)>, key='water')
Code listing

343.
344.
345.
346.
347.
348.

349.
350.
351.
352.


def __getattr__(self, key):
try:
return self.__dict__.__getitem__(str(key))
except:
raise AttributeError


def __setitem__(self, key, value):
self.__dict__.__setitem__(key, value)
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Anthony
2015-07-26 12:46:05 UTC
Permalink
First, three corrections:

- The upload field does not name the stored file the same as the name of
the uploaded file. The stored file gets renamed, with the new name
including an encoded version of the original filename (which gets decoded
upon retrieveal). It is this new filename that actually gets stored in the
upload field (the file itself is stored on the filesystem).
- The attribute after a table name is a field name, not the name of an
uploaded file, so you would do db.image.file (db.image.water makes no
sense, as the table does not include a field named "water").
- The URL argument passed to the download function to indicate which
file to download must be the filename stored in the upload field, not a
Field object.

So, to display a given image, you must somehow be able to query the
database to retrieve the image you want, and then access the "file" field
of that record to get the filename, which you will then pass as a URL arg
to the download function. For example:

In a controller:

row = db(db.image.title == 'some title').select().first()

In the view:

<img src="{{=URL('default', 'download', args=row.file)}}

Anthony
Post by michael sun
db.define_table('image',
Field('title', unique=True),
Field('file', 'upload'),
format = '%(title)s')
then upload a image called 'water'.
<img src="{{=URL('download',args=db.image.water)}}" />
<type 'exceptions.AttributeError'>()
Function argument list
(self=<Table image (id,title,file)>, key='water')
Code listing
343.
344.
345.
346.
347.
348.
349.
350.
351.
352.
return self.__dict__.__getitem__(str(key))
raise AttributeError
self.__dict__.__setitem__(key, value)
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
michael sun
2015-07-30 21:40:25 UTC
Permalink
Hi Anthony, sorry about the late reply. Thank you very much for your
detailed answers.
Post by Anthony
- The upload field does not name the stored file the same as the name
of the uploaded file. The stored file gets renamed, with the new name
including an encoded version of the original filename (which gets decoded
upon retrieveal). It is this new filename that actually gets stored in the
upload field (the file itself is stored on the filesystem).
- The attribute after a table name is a field name, not the name of an
uploaded file, so you would do db.image.file (db.image.water makes no
sense, as the table does not include a field named "water").
- The URL argument passed to the download function to indicate which
file to download must be the filename stored in the upload field, not a
Field object.
So, to display a given image, you must somehow be able to query the
database to retrieve the image you want, and then access the "file" field
of that record to get the filename, which you will then pass as a URL arg
row = db(db.image.title == 'some title').select().first()
<img src="{{=URL('default', 'download', args=row.file)}}
Anthony
Post by michael sun
db.define_table('image',
Field('title', unique=True),
Field('file', 'upload'),
format = '%(title)s')
then upload a image called 'water'.
<img src="{{=URL('download',args=db.image.water)}}" />
<type 'exceptions.AttributeError'>()
Function argument list
(self=<Table image (id,title,file)>, key='water')
Code listing
343.
344.
345.
346.
347.
348.
349.
350.
351.
352.
return self.__dict__.__getitem__(str(key))
raise AttributeError
self.__dict__.__setitem__(key, value)
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...