Treper
Treper

Reputation: 3653

Can upload form submit wait for file processing complete?

Can upload form submit wait for file processing complete?

I am using web2py and its sqlform to upload a video and in the meantime converting the video to flv .Both the progress of uploading and encoding is displayed by two progress-bar using this code(http://www.web2pyslices.com/slice/show/1337/upload-progress-in-web2py). The encoding begin at the backend once it detects video uploading. After the encoding and converting is both complete the user can play the converted video.

The problem is the form accepted when the upload is complete but the encoding is not complete.I tried the event.preventDefault() but the progress-bar do not display. It seems that the default submit can not be stopped at the time uploading is completed. How to prevent the submit and wait for the converting process is done and do the submit then?Thanks!

Upvotes: 0

Views: 621

Answers (1)

Anthony
Anthony

Reputation: 25536

You can validate the form without doing a database insert (see here). Then after validation, run a loop to continually check whether the encoding is complete, and once encoding is complete, do the database insert and return.

def video_upload():
    upload_form = SQLFORM(db.encodeupload)
    if upload_form.validate():
        session._unlock(response)
        [while loop checking to see if file encoding is complete]
        db.encodeupload.insert(**db.encodeupload._filter_fields(form.vars))
    [rest of code]

In the while loop, you might also check for errors in the encoding process and abort the insert (and return an error message) in that case.

Note, session._unlock(response) unlocks the session file before entering the loop -- this is necessary so the locked session file doesn't block the Ajax requests the page makes in order to update the encoding progress bar. In the controller action called by the Ajax requests, you should also add session.forget(response) so that action doesn't lock or write to the session.

Upvotes: 1

Related Questions