So, I did the following:
- I created a controller and I put the following code inside
# Example of a form that sends a valid POST request to changeAvatar()
@auth.requires_login()
def showChangeAvatarForm():
return locals()
# use /showChangeAvatarForm from browser for testing
@auth.requires_login()
def changeAvatar():
if not request.env.request_method == 'POST':
raise HTTP(405, json(dict(status='failure', msg='POST request is
needed')))
try: image=request.vars['image']
except: image=None
if image == None or image == "":
raise HTTP(400, json(dict(status='failure', msg='No image provided'
)))
if type(image)==type(str()):
raise HTTP(400, json(dict(status='failure', msg='Only the finename
of the image was provided, not the image itself. Try to copy the browser
behavior of /user/showChangeAvatarForm when submiting a file')))
db(db.auth_user.id==auth.user.id).update(avatar=image)
filename=db(db.auth_user.id==auth.user.id).select(db.auth_user.avatar).
first().avatar
return json(dict(status='success', msg='Avatar has been changed',
filename=filename))
- Also I created view showChangeAvatarForm.html and I put the following
code inside:
<html>
<body>
<form action="changeAvatar" method="post" enctype='multipart/form-data'>
This a demo to change avatar<br>
<input type="file" name="image"> <br>
<input type="submit"/>
</form>
</body>
</html>
- So I proved that web2py is able to accept and store images. Now I am
expecting from the front-end developer to develop an appropriate POST
request. In case that he does not know how to do it he can use this library
http://malsup.com/jquery/form/ . I have used it and I can say that it is
able to POST files that have been chosen using a html form.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
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 [email protected].
For more options, visit https://groups.google.com/d/optout.