Manual image upload

706 views
Skip to first unread message

Arun K.Rajeevan

unread,
Dec 25, 2010, 12:29:23 PM12/25/10
to web...@googlegroups.com
Assume I have a table with so many columns and one of those is an upload field.
Field('image', 'upload')

Then, I build a Form using FORM that has an item called INPUT(_name='image', _id='image', _type='file')

Now, tell me how on earth I can correctly upload  file into DB in form.onaccepts()

I'm trying many thing, but in vein. :-( 
Please, show me some light 

mdipierro

unread,
Dec 25, 2010, 12:49:12 PM12/25/10
to web2py-users
If you make a form with FROM(...INPUT(....)) the form has not
knowledge of DB and form.accepts will not perform any db-io BUT you
can do

form=FORM(INPUT(_name='image',_id='image', _type='file'),...)
if form.accepts(....):
db.mytable.insert(image=db.mytable.image.store(request.vars.image))

Arun K.Rajeevan

unread,
Dec 25, 2010, 12:54:35 PM12/25/10
to web...@googlegroups.com
restlessly trying again and again

db.pictures.insert(image = db.pictures.image.store(form.vars.image.file, form.vars.image.filename)

worked fine while I'm running web2py stand alone.

But as soon as I try to run it through GAE, it cause error.
INSERT seems working, except, file is stored some unknown place. I'm not getting reference to the file later on.

From console, these are error
-------------------------------------------------------------------------------------------------------------------------------------------------------------
INFO     2010-12-26 04:12:56,216 dev_appserver.py:3317] "GET /init/default/search/1/kj?i=0 HTTP/1.1" 200 -
ERROR    2010-12-26 04:12:58,943 restricted.py:151] Traceback (most recent call last):
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/restricted.py", line 188, in restricted
    exec ccode in environment
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/applications/init/controllers/default.py:download", line 242, in <module>
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/globals.py", line 95, in <lambda>
    self._caller = lambda f: f()
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/applications/init/controllers/default.py:download", line 229, in download
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/globals.py", line 197, in download
    (filename, stream) = field.retrieve(name)
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/dal.py", line 4259, in retrieve
    return (filename, cStringIO.StringIO(row[self.uploadfield]))
TypeError: expected read buffer, NoneType found

Arun K.Rajeevan

unread,
Dec 25, 2010, 1:00:06 PM12/25/10
to web...@googlegroups.com
db.mytable.insert(image=db.mytable.image.store(request.vars.image))  
will not work
---------------------------------------------------------------------------------------------------------------------------------------------
Traceback (most recent call last):
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/restricted.py", line 188, in restricted
    exec ccode in environment
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/applications/init/controllers/default.py", line 242, in <module>
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/globals.py", line 95, in <lambda>
    self._caller = lambda f: f()
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/applications/init/controllers/default.py", line 121, in search
    id = db.pictures.insert(image = db.pictures.image.store(request.vars.image),words=[id], entry_by=who)
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/dal.py", line 4238, in store
    shutil.copyfileobj(file, dest_file)
  File "/usr/lib/python2.6/shutil.py", line 27, in copyfileobj
    buf = fsrc.read(length)
  File "/usr/lib/python2.6/cgi.py", line 522, in __getattr__
    raise AttributeError, name
AttributeError: read

mdipierro

unread,
Dec 25, 2010, 1:02:26 PM12/25/10
to web2py-users
Strange. Can you plese

print db.pictures.fields()
print db.pictures.image.uploadfield
print db._adapter.uploads_in_blob

what's the output?



In gluon/dal.py there is this line:

if db and self._db._adapter.uploads_in_blob==True or
for field in fields:
if isinstance(field, Field) and field.type == 'upload'\
and field.uploadfield is True:
tmp = field.uploadfield = '%s_blob' % field.name
fields.append(self._db.Field(tmp, 'blob',
default=''))

It should add a blob field to the pictures table.

Arun K.Rajeevan

unread,
Dec 25, 2010, 1:05:27 PM12/25/10
to web...@googlegroups.com
web2py Shell Version 1.91.4 (2010-12-24 10:54:48)
In[0]:
print db.pictures.fields() 
Out[0]:
['id', 'image', 'words', 'rating', 'entry_by']
In[1]:
print db.pictures.image.uploadfield 
Out[1]:
True
In[2]:
print db._adapter.uploads_in_blob 
Out[2]:
False

Arun K.Rajeevan

unread,
Dec 25, 2010, 1:35:33 PM12/25/10
to web...@googlegroups.com
Oh, silly me.
It took me some time to go through dal.py and realize I made a mistake by running above commands in web2py shell.

There db.adapter will be SQLite and I've problem with gae.

Now, here is the actual result.

print db.pictures.fields()
 ['id', 'image', 'words', 'rating', 'entry_by', 'image_blob']

print db.pictures.image.uploadfield 
image_blob

print db._adapter.uploads_in_blob 
True


mdipierro

unread,
Dec 25, 2010, 1:52:01 PM12/25/10
to web2py-users
So it works now?

On Dec 25, 12:35 pm, "Arun K.Rajeevan" <the1.a...@gmail.com> wrote:
> Oh, silly me.
> It took me some time to go through dal.py and realize I made a mistake by
> running above commands in web2py shell.
>
> There db.adapter will be SQLite and I've problem with gae.
>
> Now, here is the actual result.
>
> print db.pictures.fields()
> * ['id', 'image', 'words', 'rating', 'entry_by', 'image_blob']*
>
> print db.pictures.image.uploadfield
> *image_blob*
>
> print db._adapter.uploads_in_blob
> *True*

mdipierro

unread,
Dec 25, 2010, 1:59:18 PM12/25/10
to web2py-users
this:

id = db.pictures.insert(image =
db.pictures.image.store(request.vars.image),words=[id], entry_by=who)

should be

id = db.pictures.insert(image =
db.pictures.image.store(request.vars.image.file),words=[id],
entry_by=who)

Arun K.Rajeevan

unread,
Dec 25, 2010, 2:02:08 PM12/25/10
to web...@googlegroups.com
As I said, Insert seems working. since, form.accepts work.
Error occurs, when I'm trying to retrieve it.

url = URL(r=request, f='download', args=[item.image]) 
IMG(_src=url, _width='150px', _height='125px')

this is how I shows picture  on page.
When run through GAE it doesn't shows picture. (it works fine, when run alone (server with web2py installation))

-----------------------------------------------------------------------------------------------------------------------------------------------
INFO     2010-12-26 05:08:19,434 dev_appserver.py:3317] "GET /init/default/download/pictures.image.9fbe6db6cf273fd6.45323632312e4a5047.JPG HTTP/1.1" 500 -

and the error (that I posted before) occures

mdipierro

unread,
Dec 25, 2010, 2:07:01 PM12/25/10
to web2py-users
I understand now. I think the problem is that the field was added
later and there are record that contain None instead of ''. I fixed
this in trunk, please check it.

massimo

Arun K.Rajeevan

unread,
Dec 25, 2010, 2:11:59 PM12/25/10
to web...@googlegroups.com
This worked with web2py alone, but through GAE following error occured

ERROR    2010-12-26 05:35:43,554 restricted.py:151] Traceback (most recent call last):
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/restricted.py", line 188, in restricted
    exec ccode in environment
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/applications/init/controllers/default.py:search", line 242, in <module>
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/globals.py", line 95, in <lambda>
    self._caller = lambda f: f()
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/applications/init/controllers/default.py:search", line 121, in search
  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/visuallingua/gluon/dal.py", line 4208, in store
    filename = file.name
AttributeError: 'cStringIO.StringO' object has no attribute 'name'


mdipierro

unread,
Dec 25, 2010, 2:14:34 PM12/25/10
to web2py-users
Please show he entire action.

Arun K.Rajeevan

unread,
Dec 25, 2010, 2:16:22 PM12/25/10
to web...@googlegroups.com
I'm going to check trunk, but I'm curious to know the difference b/n

image = db.pictures.image.store(form.vars.image.file, form.vars.image.filename)
and
image = db.pictures.image.store(request.vars.image.file)

first one has two arguments and later has only one.( so, file name is not mandatory?)

And I think I'm correct in assuming that request.vars will be copied to form.vars ?

mdipierro

unread,
Dec 25, 2010, 2:18:59 PM12/25/10
to web2py-users
Actually... this is correct

image = db.pictures.image.store(form.vars.image.file,
form.vars.image.filename)

this is not correct (my mistake)

image = db.pictures.image.store(request.vars.image.file)

but this is correct again because the file object carries a filename

image = db.pictures.image.store(open(filename,'rb'))

Arun K.Rajeevan

unread,
Dec 25, 2010, 2:50:42 PM12/25/10
to web...@googlegroups.com
Updated from trunk.
run again.

Still GAE shows error. Now page is not rendering at all. (before page showed without image)

ERROR    2010-12-26 06:08:42,649 restricted.py:151] Traceback (most recent call last):  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/Test/gluon/restricted.py", line 188, in restricted    exec ccode in environment  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/Test/applications/init/controllers/default.py:search", line 242, in <module>  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/Test/gluon/globals.py", line 95, in <lambda>    self._caller = lambda f: f()  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/Test/applications/init/controllers/default.py:search", line 121, in search  File "/media/KRA/Evolve/Work/web2py_src_downloaded/google_appengine/Test/gluon/dal.py", line 4210, in store    filename = file.nameAttributeError: 'cStringIO.StringO' object has no attribute 'name'

mdipierro

unread,
Dec 25, 2010, 2:56:16 PM12/25/10
to web2py-users
Arun, did you read this?

Arun K.Rajeevan

unread,
Dec 25, 2010, 2:59:01 PM12/25/10
to web...@googlegroups.com
I changed 
 image = db.pictures.image.store(form.vars.image.file, form.vars.image.filename) 
to 
  image = db.pictures.image.store(request.vars.image.file) 

for above post.

Now I changed them back. (ie correct thing)
Now there's no error in console. 
But image is not showing up.

directly.

For my surprise, it showed up and I saved the file on hdd and opened it.
my image viewer shows 
Error interpreting JPEG image file (Improper call to JPEG library in state 200)

Is that mean, file get corrupted in some state of uploading to db. (conversion)

mdipierro

unread,
Dec 25, 2010, 3:01:21 PM12/25/10
to web2py-users
Arun, we cannot continue like this. I need to see the entire model and
actions that do upload/download.

Perhaps something is wrong with web2py but you are not providing a way
to reproduce the problem.

Massimo

On Dec 25, 1:59 pm, "Arun K.Rajeevan" <the1.a...@gmail.com> wrote:
> I changed
>  image =
> db.pictures.image.store(form.vars.image.file, form.vars.image.filename)
> to
>   image = db.pictures.image.store(request.vars.image.file)
>
> for above post.
>
> Now I changed them back. (ie correct thing)
> Now there's no error in console.
> But image is not showing up.
>
> And I tried to download the
> filehttp://localhost:8080/init/default/download/pictures.image.bddae63fff...

Arun K.Rajeevan

unread,
Dec 25, 2010, 3:08:08 PM12/25/10
to web...@googlegroups.com
https://groups.google.com/d/msg/web2py/UtBmAdvY7E0/QQ5rBVM4lEIJ

tables are given in that post.

But, as I point out last, problem may be in conversion of file into blob and getting it back.
BTW, if you can suggest a solution discussed in above link that will be great; although this problem is amusing, I'm in need for a solution.

mdipierro

unread,
Dec 25, 2010, 3:15:54 PM12/25/10
to web2py-users
Email me your app.

Arun K.Rajeevan

unread,
Dec 25, 2010, 3:32:48 PM12/25/10
to web...@googlegroups.com
It's on the way :)
Thank you for your precious time.

Arun K.Rajeevan

unread,
Dec 25, 2010, 4:14:17 PM12/25/10
to web...@googlegroups.com
tried to upload to appspot.
on admin panel, I tried to view record I just inserted.

id=53018 118005 pictures.image.979a10271e1b65a7.6c7563696665722d2e6a7067.jpg <null> 0.0 [118014L]

It shows, image field is populated, but blob field is null.

(thought, it may be helpful to debug )

王康

unread,
May 10, 2012, 4:19:00 PM5/10/12
to web...@googlegroups.com
hi, have you guys solved this problem?

Because now I'm in the same problem.... >_<

mannu kumar

unread,
Feb 12, 2014, 12:17:52 AM2/12/14
to web...@googlegroups.com

db.mytable.insert(image=db.mytable.image.store(request.vars.image.file, request.vars.image.filename), image_blob = request.vars.image.file.read()) 


this should work,

ref http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#More-on-uploads

<<

Note, if the file is to be stored in an associated blob field rather than the file system, the store() method will not insert the file in the blob field (because store() is called before the insert), so the file must be explicitly inserted into the blob field:

>>> db.define_table('myfile',
        Field('image', 'upload', uploadfield='image_file'),
        Field('image_file', 'blob'))
>>> stream = open(filename, 'rb')
>>> db.myfile.insert(image=db.myfile.image.store(stream, filename),
        image_file=stream.read())

>>
Reply all
Reply to author
Forward
0 new messages