Search

Jason Zerbe's draftbook | building digital products and services

html5 FormData multipart upload to django backend

11 May 2013

FormData
As of Chrome 7, Firefox 4, IE 10, Opera 12, and Safari 5 - it is quite possible to programmatically upload file data. [1] From SBObj.js:
this.PUT = function(theData, theDataContentType) {
    theDataContentType = this.getDefault(theDataContentType, "text/plain");
    var aBlob = new Blob([ theData ], {
        type: theDataContentType
    });

    var aFormData = new FormData();
    aFormData.append("file", aBlob);
    var aDataObj = {
        "type": false,
        "content": aFormData
    };

    private_SendDataObj(private_METHOD_POST, aDataObj);
};

Where private_SendDataObj is essentially a wrapper for xhr.send(aDataObj.content);. Be sure that there are NO setRequestHeader method calls on the xhr object; even though the django: File Uploads documentation tells you otherwise.
django's request.FILES
On the server-side, well ... Google App Engine with django-nonrel to be specific, we can pull in the file data. From storagebin/__init__.py and storagebin/internal/blobstore.py:
elif request.method == 'POST':
    if request.FILES and len(request.FILES) == 1:
        uploaded_file = request.FILES['file']

        ...

        # write new data
        file_name = files.blobstore.create(uploaded_file.content_type)
        with files.open(file_name, 'a') as f:
            f.write(uploaded_file.read())
        files.finalize(file_name)
refs: HttpRequest.FILES, class UploadedFile, files.blobstore.create

django enable Cross Origin Resource Sharing

05 May 2013

I am using a django-nonrel setup, and I needed to enable CORS. Relevant reading:
add items to HttpResponse dictionary
From the django manual:
To set or remove a header in your response, treat it like a dictionary:
response = HttpResponse()
response['Cache-Control'] = 'no-cache'
del response['Cache-Control']

function pastie
def addCORSHeaders(theHttpResponse):
    if theHttpResponse and isinstance(theHttpResponse, HttpResponse):
        theHttpResponse['Access-Control-Allow-Origin'] = '*'
        theHttpResponse['Access-Control-Max-Age'] = '120'
        theHttpResponse['Access-Control-Allow-Credentials'] = 'true'
        theHttpResponse['Access-Control-Allow-Methods'] = 'HEAD, GET, OPTIONS, POST, DELETE'
        theHttpResponse['Access-Control-Allow-Headers'] = 'origin, content-type, accept, x-requested-with'
    return theHttpResponse
From storagebin/__init__.py

Flex 4.5 SDK debugging optimizations

05 Feb 2013

max the JVM heap
Open the file bin/jvm.config in the root of your SDK install. Update the java.args line to look like java.args=-Xms512m -Xmx1024m -Dsun.io.useCanonCaches=false. Even on one of the latest MacBook Pros with ~2.8x4 GHz and 16GB of RAM, the debugger would barf if I told it to allocate more.

modify Firefox Flash plugin timeout
Enter about:config in the URL bar. The field we are going to modify is called dom.ipc.plugins.timeoutSecs; the default is 45 (seconds) which is not nearly enough of an outing for a decent break point gala event. I like having mine set to 600 (10 minutes).