Something like this:

def upload():
    import os
    uploadfolder=os.path.join(request.folder, 'uploads')
    form = SQLFORM.factory( 
        Field('file', 'upload', uploadfolder=uploadfolder),
        Field('new_name'))
    if form.process().accepted:
        os.rename(os.path.join(uploadfolder, form.vars.file),
            os.path.join(uploadfolder, form.vars.new_name))
    return dict(form=form)

The above uses SQLFORM.factory, though you could also do it using FORM. 
Rather than handling the upload completely manually, this code allows 
web2py to use its usual upload mechanism and automatic file naming, and 
then it simply renames the uploaded file to the name you want (this code 
allows you to enter the new filename in the form itself, though you could 
use some other mechanism for generating the name).

Note, because you're not storing the filename in a db table and not using 
the standard naming scheme, you won't be able to use the 
response.download() method for downloading, though it sounds like you don't 
need to. Instead, you can use response.stream() if necessary.

Anthony



On Saturday, November 19, 2011 2:14:26 PM UTC-5, miroslavgojic wrote:
>
> Can I get full example for upload function?
>
> This is maybe simply but after one day I don'n have any success.
>
> - - Miroslav Gojic - -
>
> On Sat, Nov 19, 2011 at 19:24, Anthony <abas...@gmail.com> wrote:
>
>> Don't put the form.accepts inside the 'if request.vars' block -- it needs 
>> to run even on form creation (to generate the hidden formname and formkey 
>> fields).
>>
>>
>> On Saturday, November 19, 2011 12:44:31 PM UTC-5, miroslavgojic wrote:
>>
>>> Now I have in controller:
>>> def upload():
>>>     form = FORM("Upload
>>> file:",INPUT(_type='file',_**name='myfile'),INPUT(_type='**
>>> submit',_name='submit',_value=**'Submit'))
>>>     if request.vars:
>>>         if form.accepts(request,session):
>>>             my_file = request.vars.myfile.file
>>>             my_filename = request.vars.myfile.filename
>>>         filepath = os.path.join(request.folder, 'uploads') // this
>>> path work - it is absolute path in hard drive
>>>         fp =open(filepath.my_filename,'**wb')
>>>         fp.write(my_file)
>>>         fp.close()
>>>     return dict(form=form)
>>>
>>> request.vars.myfile -> make return on stored object
>>> request.vars.myfile.file -> make return address of stored object
>>>
>>> I understood what you told me, and logical check on conditions, but
>>> how to put everything in one function.
>>>
>>> the pseudo algorithm in my head is next:
>>>
>>> make def func():
>>>     make form = FORM(...)
>>>     check if condition existing
>>>         make file = request.vars.myfile.file
>>>         make filename = request.vars.myfile.filename
>>>         make filepath = os.path.join(...)
>>>         make write file to filepath filename
>>>         go to page and show empty form - wait for new file
>>>     else:
>>>         just show empty form without submission
>>>     return form
>>>
>>> but I just loss my mind after 24 ours of trying this or similar
>>> uploads.
>>>
>>> Miroslav
>>>
>>> On Nov 19, 6:12 pm, Anthony <aba...@gmail.com> wrote:
>>> > You can tell if the function is being called with a form submission by
>>> > checking for request.vars:
>>> >
>>> > if request.vars:
>>> >     print 'this is a form submission'
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > On Saturday, November 19, 2011 12:06:40 PM UTC-5, miroslavgojic wrote:
>>> >
>>> > > The error is caused when file is not selected.
>>> > > By default on first run form is empty (file is not selected), and 
>>> form
>>> > > must wait for selecting and submitting.
>>> >
>>> > > How access to file before calling form? What that mean?
>>> >
>>> > > Miroslav
>>> >
>>> > > On Nov 19, 5:52 pm, Anthony <aba...@gmail.com> wrote:
>>> > > > You might need to access the file before calling form.accepts 
>>> (first
>>> > > you'll
>>> > > > have to check that form.vars.myfile exists). You can also access 
>>> it via
>>> > > > request.vars.myfile (which won't change, even after form.accepts).
>>> >
>>> > > > Anthony
>>>
>>>
>

Reply via email to