Hi,

I finally figured out to do a file upload via curl (may be obvious to 
everybody else, but not to me ...) so here we go:

I followed the 'manual file upload' example over at web2pyslices (thanks, 
Yarin!):
  http://www.web2pyslices.com/slice/show/1504/manual-uploads

Just for clarity I changed the table name and the field names to 't_*' and 
'f_*' (which I consider a best practice).
I added this to the model ...
db.define_table('t_image',
    Field('f_title'),
    Field('f_file', 'upload'),
    format = '%(title)s')

... and this to the controller ...
@auth.requires_login()
def index():    
    image_form = FORM(
        INPUT(_name='image_title',_type='text'),
        INPUT(_name='image_file',_type='file')
        )
    
    if image_form.accepts(request.vars,formname='image_form'):
        
        image = db.t_image.f_file.store(image_form.vars.image_file.file, 
image_form.vars.image_file.filename)
        id = 
db.t_image.insert(f_file=image,f_title=image_form.vars.image_title)
    
    images = db().select(db.t_image.ALL)
    
    return dict(images=images)
... and this to the view.

{{extend "layout.html"}}
 
<form action="" enctype="multipart/form-data" method="post">
    <input name="_formname" type="hidden" value="image_form">
    <input class="string" name="image_title" type="text" value="">
    <input class="upload" name="image_file" type="file">
    <input type="submit" value="Submit">
</form>
 
<ul>
{{for image in images:}}
    <li>
        <a href="{{=URL(f='link', args=image.f_file)}}">
            <img src="{{=URL(f='link', args=image.f_file)}}"/>
            {{=image.f_title}}
        </a>
        <a href="{{=URL(f='download', args=image.f_file)}}">
            [Download]
        </a>
    </li>
{{pass}}
</ul>


The applications name was 'upload' so the corresponding cURL call would be:
curl --user user:pass --form "image_title=some name" --form 
"_formname=image_form" --form "[email protected]" --form Submit=OK http:
//127.0.0.1:8000/upload/default/index
(with user:pass being substituted with a valid username/password 
combination and test.png sitting in the same directory)

I hope this saves somebody some time.

This doesn't work for the SQLFORM.smartgrid forms unfortunately since they 
sport a '_formkey' parameter which changes every time you call the form. 
 If someone knows a solution to this, I'd be happy to learn it.

-- 



Reply via email to