The download() function calls response.download() to find and serve the 
file, and response.download() gets the database table and field names from 
the filename in the URL. SQLFORM.factory works by creating a dummy DAL 
instance with a dummy DAL Table, and by default it names that table 
"no_table", which is then used in constructing the filename, as you can see 
in the URL below. Of course, there is no "no_table" table, so 
response.download() can't find the file. To get around this problem, you 
can explicitly specify the name of the dummy table so it is the same as the 
name as the real table containing the upload field:

    form = SQLFORM.factory(
        Field('description', 'string'),
        Field('mypicture', 'upload', uploadfolder=os.path.join(request.
folder, 'uploads/')),
        table_name='mypictures')

With that change, the filenames will begin with "mypictures" instead of 
"no_table", and response.download() should then be able to locate the real 
database table and properly serve the file.

Anthony

On Friday, February 8, 2013 7:21:50 AM UTC-5, Dragan Matic wrote:
>
> Here is a model table for holding picture names:
>
> db.define_table('mypictures', 
>     Field('description', 'string'),
>     Field('mypicture', 'upload'))
>
> If I try to upload a picture through simple SQLFORM everything works ok, 
> picture is renamed to something like 
> 'mypictures.mypicture.be0ff92956b815ab.70756c6933312e6a7067.jpg' and I can 
> show it in view. 
>
> def SendPicture():   
>     form=SQLFORM(db.mypictures)
>     
>     if form.accepts(request, session):
>         response.flash = "picture uploaded"
>     
>     return dict(form=form)
> This is the created html and everything works ok, I can show it or 
> download it:
> <img src=
> "/showpics/default/download/mypictures.mypicture.be0ff92956b815ab.70756c6933312e6a7067.jpg"
>  
> width="300" />
>
>
> However if I try to upload a picture through SQLFORM.factory and then 
> insert values into database and then if I try to show it or download it 
> web2py responds with "400 not found" although picture is uploaded. 
>
> def SendPicture():   
>     form = SQLFORM.factory(
>         Field('description', 'string'),
>         Field('mypicture', 'upload', uploadfolder=os.path.join(request.
> folder, 'uploads/')))
>     
>     if form.accepts(request, session):
>         response.flash = "picture uploaded"
>         db.mypictures.insert(description=form.vars.description, mypicture=
> form.vars.mypicture)
>
>     return dict(form=form)
>
> This is the html code created, picture is not shown and if I try to 
> download it, I get a '400 not found' response although the picture exists 
> on a given location.
>
> <img src=
> "/showpics/default/download/no_table.mypicture.80201cf3260698b3.70756c695f73615f6a657a696b6f6d322e6a7067.jpg"
>  
> width="300" />
>
>
>
>
>

-- 

--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to