Form upload troubles when AJAX=True on component

28 views
Skip to first unread message

Robert Porter

unread,
Jul 3, 2015, 12:00:22 PM7/3/15
to web...@googlegroups.com
This first part works fine on its own.  It's when I try to make this part of an AJAX component inside another page that it fails.

Model:
db.define_table('temp_pics',
Field('image', 'upload', required=True, notnull=True, requires=IS_IMAGE()))

Controller:
# This will reload itself and allow changing the photo after submission.
def upload_temp():
record = db.temp_pics(request.args(0))
pic_upload = SQLFORM(db.temp_pics, record, upload=URL('download'))
if pic_upload.process(formname='pic_upload').accepted:
redirect(URL('upload_temp', args=id),client_side=True)
return dict(pic_upload=pic_upload)

View:
{{=pic_upload}}

Now I want to put this in a different view using:
{{=LOAD('default', 'upload_temp','upload_temp.load',ajax=True)}}

If I set ajax=False, it will work, but reload the whole page, which fully defeats the purpose.

Somewhere you need to have this script referenced (and you'll have to download that script online first):
src="{{=URL('static', 'js/jquery.form.js')}}"

And in the default/upload_temp.html (and identical upload_temp.LOAD), you need to have this:

{{=pic_upload}}
<script>
//<!--
$('document').ready(function() {
$('pic_upload').ajaxForm({dataType: 'script', // <-- like the ':eval' target in ajax() function
url: "{{=URL(args=pic_upload.vars.id)}}" // <-- put form controller action here
});
});
</script>


Is there a way to make this work?  The book just says AJAX doesn't do forms, but doesn't go on to explain how I should handle this sort of thing.  Any help is much appreciated!

黄祥

unread,
Jul 3, 2015, 1:45:09 PM7/3/15
to web...@googlegroups.com
i think it's related with the ajax and the table field type (upload)
ref:
http://web2py.com/books/default/chapter/29/12/components-and-plugins#Ajax-post-does-not-support-multipart-forms

best regards,
stifan

Robert Porter

unread,
Jul 4, 2015, 7:45:25 AM7/4/15
to web...@googlegroups.com
That's an answer, but not a solution.  I think I found a workaround.  Doing some final testing and will post again if I get a proper solution.

Picture sharing is one of the fundamental pillars of the web.  It's very disappointing that web2py doesn't have a definitive solution to this (after 3 days of searching, I can say that with confidence).  There are many google/SO hits with attempted solutions but none of them just work.
Reply all
Reply to author
Forward
0 new messages