Hello Ross,

Here is a bunch of info from another post I made regarding manual
uploads with SQLFORM.factory.

Now that I notice it though, you are using FORM and I'm uncertain
whether or not FORM worked for me, I had to use SQLFORM.

The whole thread can be found here for more info, relevant stuff
posted below:

http://groups.google.com/group/web2py/browse_thread/thread/fe03404c155aaf10/f9cb6c1ac67c3f49?lnk=gst&q=thesweetlink#f9cb6c1ac67c3f49

Two things I've found when manually uploading via SQLFORM.factory:

1)  You need to specify a table_name='...' to avoid the
no_table_newfilename.extension issue like this:

form = SQLFORM.factory(...Field definitions...,
table_name='some_table_name')

2)  Additionally you must specify an uploadfolder in your upload Field
definition similar to this:

form = SQLFORM.factory(...,
Field('invoice_logo', type='upload',
uploadfolder=os.path.join(request.folder,'static/uploads/')),
..., table_name='whatever_you_like')

**NOTE** 'static/uploads' is just an example, you can upload to
wherever it will be appropriate.

In this case the newly uploaded and renamed file to
your_application's_dir/static/uploads/your_new_filename_here

One gotcha to look out for following your field name as an example
without the quotation marks:

In your form.accepts(...):

"request.vars.invoice_logo" will contain the original filename of your
upload whereas

"form.vars.invoice_logo_newfilename" will contain the newly renamed
file like yourtablename.9203842903.thaoeu09gu023hgda3p.ext

No need to call store() directly as SQLFORM.factory will take care of
that for you.

I hope that this helps you.

David

On Oct 28, 10:38 am, Ross Peoples <ross.peop...@gmail.com> wrote:
> Having a little trouble with getting an attachment to work. This is a
> simplified version of the controller:
>
> form = FORM('Attach', [INPUT(_name='attachment', _type='file')])
>
> if form.accepts(request):
>     v = form.vars.attachment
>     if not v is None and not v == '':
>         v = db.attachments.attachment.store(v)
>
>     db(db.attachments.id==id).update(attachment = v)
>
> And this is the result:
>
> Traceback (most recent call last):
>   File "/media/psf/Python/web2py/gluon/restricted.py", line 194, in restricted
>     exec ccode in environment
>   File "/media/psf/Python/web2py/applications/marlin/controllers/default.py" 
> <https://172.16.100.111:8000/admin/edit/marlin/controllers/default.py>, line 
> 586, in <module>
>   File "/media/psf/Python/web2py/gluon/globals.py", line 149, in <lambda>
>     self._caller = lambda f: f()
>   File "/media/psf/Python/web2py/applications/marlin/controllers/default.py" 
> <https://172.16.100.111:8000/admin/edit/marlin/controllers/default.py>, line 
> 471, in view_invite
>     v = db.rfq_item[field].store(v)
>   File "/media/psf/Python/web2py/gluon/dal.py", line 5557, 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
>
> The code for using store(v) came from another thread. When I print out v 
> before calling store(), it prints out something like this:
>
> FieldStorage('attachment', 'filename.txt', 'The actual text of the file')
>
> Looking through the dal.py, it looks like the line "shutil.copyfileobj(file, 
> dest_file)" is expecting an actual File object, not a FieldStorage object, 
> hence the attribute error. But if I try to pass "storage(v.file)" instead of 
> "storage(v)", then the DAL errors out because it's expecting a FieldStorage 
> object, not a File object.
>
> Is this a bug or am I doing something wrong here? I should mention that using 
> SQLFORM and SQLFORM.factory with attachments works just fine, it's only FORM 
> that is giving me trouble.

Reply via email to