HTML, Jquery, Python – Form File Upload (GAE)

I’ve looked through many questions, but I cannot seem to find one addressing my specific issue.

I am attempting to allow users (on my Google App Engine website) to upload a video to wistia (video host).

I was able to get it to work by directly opening the file, but this only works if the file is already in the app folder:

class VideoUploadHandler(webapp2.RequestHandler):
    def post(self):
        title = self.request.get('title')
        video = self.request.get('video')
        description = self.request.get('description')

        url = 'http://upload.wistia.com'
        params = {
            'api_password'    : 'my_password',
            'file'            : open(video, "rb"),  #<--- HOW TO USE FILE OBJECT?
            'project_id'      : 'my_project_id',
            'name'            : title,
            'description'     : description,
            }
        opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
        response = opener.open(url, params)

        logging.warning(response.read())

        self.redirect("http://www.my_webpage.com/")

This works if the variable ‘video’ is merely the file name and the file is in the same folder, but I obviously need to allow users to select any file, so I changed my HTML to this:

HTML:

<form enctype="multipart/form-data" action="/video_upload/" method="post">
<p>Title: <input type="text" name="title"/></p>
<p><input type="file" name="video"/></p>
<p>Description:<br><textarea rows="4" cols="50" name="description"></textarea></p>
<p><input type="submit" value="Upload"/></p>
</form>

However, I am having trouble understanding how to use the file object in my POST request. For some reason, I’m not able to glean this information from other answers already posted.

How do I structure my ‘params’ using this HTML Form method?

If possible, I would also prefer to use AJAX, since I have to redirect the webpage to reload using the Form method. I am also not understanding how to do that and pass the file object to Python:

$.ajax({
    type: "POST",
    url: "/video_upload/",
    dataType: 'json',
    data: JSON.stringify({'video': video, 'title': title, 'description', description})
});

The answers I keep finding complete the entire POST request in JavaScript, but I want to pass the file object to Python first.


Download html.jquery.python.form.file.upload.gae.zip
Direct Link


Download


Download html.jquery.python.form.file.upload.gae.zip
Mediafire


Download


Download html.jquery.python.form.file.upload.gae.zip
Hotfile


Download

Both comments and pings are currently closed.

Comments are closed.