uploading a file with a form ( without SQLFORM)

40 views
Skip to first unread message

Multiplery

unread,
Apr 13, 2018, 4:15:36 PM4/13/18
to web2py-users
Hey folks,

I'm attempting to upload a file directly without the use of a SQLForm but rather the ordinary FORM.
From previous threads regarding the topic i figured my controller should look something like this:

[code]
def index():
    
    import shutil 
    form = FORM( 
            INPUT( _name="upload", _type="file"),
        INPUT( _type="Submit", _value="Upload" ))
    if form.accepts(request,session):
        if form.vars: 
         
           filename=request.vars.upload.filename
           file=request.vars.upload.file
           shutil.copyfileobj(file,open('/path'+filename,'wb')) 
    return dict (form = form)
[/code]

yet no file appears in the folder of my path. I'm not sure if the problem is with my code or maybe elsewhere, for example permission based. I'm using a raspberry with a raspiban os. i Already tried to start python as root user. 

Can someone spot a mistake in the code? has someone tried to run this on a debian based os and knows if its a permission problem? I'm aware of the potential dangers of the shutil.copyfileobj, so no worries about that.

Sincerely,

Dave S

unread,
Apr 17, 2018, 3:26:15 AM4/17/18
to web2py-users
Shouldn't you be passing the [relevant] request.post_vars to form.accepts() ?  See the top-level paragraph for FORM in chapter 7, and also

/dps

Reply all
Reply to author
Forward
0 new messages