Archive for June, 2009

Handling File Uploads from a Flex client using Python

Thursday, June 18th, 2009

Handling file uploads in python is fairly simple.

The server receives a file object which contains both the file’s name and its data content.

Reading the file object depends on the html form that sends the request to the server:

<form enctype=”multipart/form-data” action=”save_file.py” method=”post”>
<p>File: <input type=”file” name=”filename”></p>
<p><input type=”submit” value=”Upload”></p>
</form>

Since, in this example, the input variable whose type is “file” is named “filename”, the file object is accessed by reading form["filename"] on the server.

The file’s name string is obtained by reading the form["filename"].filename attribute, and form["filename"].file contains the actual file data.

Of course, “filename” is arbitrary, and can be anything, as long as the server is synced with the client form.

Or so I thought until I tried handling a file upload from an Adobe Flex client.

Flex doesn’t bother with html forms, of course, and has its own logic for handling file uploads from the client.

What’s not clear in those docs (though it is referenced somewhat opaquely in the FileReference class sample http post) is that Flex will send the file object as “Filedata”.

So the python code on the server must use form["Filedata"] or the upload won’t work.