Re: Upload form, onvalidation and postbacks

220 views
Skip to first unread message

Massimo Di Pierro

unread,
Dec 21, 2012, 2:27:36 AM12/21/12
to web...@googlegroups.com
Which web2py version? I believe this was fixed in 2.2 or 2.3.

Massimo

On Thursday, 13 December 2012 11:55:34 UTC-6, Andy Salnikov wrote:
Hi,

I have an issue with the upload form and postbacks. In my application I need to validate file contents before accepting it from user upload form, so naturally I try to use "onvalidation" method to it. The table used in the forma is defined like this:

db.define_table('input_data',
                Field('input_file', 'upload', autodelete=True, requires=IS_NOT_EMPTY(), label=T('Input file')),
                Field('output_type', default="xxx", requires=IS_IN_SET(['xxx', 'yyy']), label=T('Output format')),
                Field('original_name', writable=False, readable=False),
                Field('created', 'datetime', writable=False, readable=False),
                )


My simplified action looks like this:

def _validateFileForm(form):
    try:
        validator.validate(form.vars.input_file.file)
        # on success set hidden fields
        form.vars.original_name = request.vars.input_file.filename
        form.vars.created = datetime.datetime.now()
    except Exception, ex:
        # if validation fails then display an error
        form.errors.input_file = T('file_validation_failed') + ': ' + str(ex)

def index():

    form = SQLFORM(db.input_data, submit_button=T("Upload"))
    if form.process(onvalidation=_validateFileForm).accepted:
        # save file id in a session and go to options page
        session.input_data_id = form.vars.id
        redirect(URL(options_odt))

    return dict(form=form)


Validation seems to work OK, if file is OK then everything is redirected to next page, if file is bad then message is displayed in red under file upload field.

What is interesting about it is that when validation fails and form is re-displayed it also looks like that the whole file contents is send back to the client together with the form. For large uploaded file this could be a lot of data. I see long delays and when I check the HTTP response headers I see many-megabytes number there. Is there a way to avoid sending the file contents back with the form during a postback?

Thanks,
Andy

Andy Salnikov

unread,
Dec 23, 2012, 1:01:28 PM12/23/12
to web...@googlegroups.com
Hi Massimo,

the problem happened with 2.1. I have just upgraded to 2.3.2 but I do not see any change. Below are the request/response headers for the upload form that results in validation failure. I tried to upload You can see that I tried to upload an image (file size is 1,127,797 bytes), for that image request Content-Length is 1,128,340 bytes but response is 3,445,392 bytes (3 times large than request?)

Let me know if I can provide more info for you.

Thanks,
Andy

    1. Request URL:
    2. Request Method:
      POST
    3. Status Code:
      200 OK
    4. Request Headers
      1. POST /famtreepub/default/index HTTP/1.1 Host: 127.0.0.1:8000 Connection: keep-alive Content-Length: 1128340 Cache-Control: max-age=0 Origin: http://127.0.0.1:8000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 Content-Type: multipart/form-data; boundary=----WebKitFormBoundarycW2MnFYBYRq4arq9 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Referer: http://127.0.0.1:8000/famtreepub/default/index Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: session_id_admin=127.0.0.1-33b3e06f-388e-4f4b-ab89-a106d87c67c0; session_id_famtreepub=127.0.0.1-06dbb028-2086-4cc3-a568-77f069922d8c
    5. Request Payload
      1. ------WebKitFormBoundarycW2MnFYBYRq4arq9 Content-Disposition: form-data; name="input_file"; filename="drevo.jpg" Content-Type: image/jpeg ------WebKitFormBoundarycW2MnFYBYRq4arq9 Content-Disposition: form-data; name="output_type" OpenDocument ------WebKitFormBoundarycW2MnFYBYRq4arq9 Content-Disposition: form-data; name="_formkey" 23e17674-c5fc-43a4-92e5-840cdd0598d4 ------WebKitFormBoundarycW2MnFYBYRq4arq9 Content-Disposition: form-data; name="_formname" input_data/create ------WebKitFormBoundarycW2MnFYBYRq4arq9--
    6. Response Headers
      1. HTTP/1.1 200 OK X-Powered-By: web2py Set-Cookie: session_id_famtreepub=127.0.0.1-06dbb028-2086-4cc3-a568-77f069922d8c; Path=/ Expires: Sun, 23 Dec 2012 16:42:55 GMT Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Content-Type: text/html; charset=utf-8 Date: Sun, 23 Dec 2012 16:42:55 GMT Server: Rocket 1.2.6 Python/2.7.2 Content-Length: 3445392 Connection: keep-alive

Massimo Di Pierro

unread,
Dec 24, 2012, 12:34:38 AM12/24/12
to web...@googlegroups.com
I think this is now fixed in trunk. Please check it.

Andy Salnikov

unread,
Dec 27, 2012, 12:55:35 AM12/27/12
to web...@googlegroups.com
Hi Massimo,

thanks, I checked out trunk version from today and it is indeed fixed now.

Happy holidays,
Andy
Reply all
Reply to author
Forward
0 new messages