Search code examples
pythonfile-uploadcontent-typewebapp2

Webapp2 request application/octet-stream file upload


I am trying to upload a file to s3 from a form via ajax. I am using fineuploader http://fineuploader.com/ on the client side and webapp2 on the server side. it sends the parameter as qqfile in the request and I can see the image data in the request headers but I have no idea how to get it back in browsers that do not use the multipart encoding.

This is how I was doing it in the standard html form post with multipart.

image = self.request.POST["image"]

this gives me the image name and the image file

currently I have only been able to get the image filename back not the data with

image = self.request.get_all('image')
[u'image_name.png']

when using the POST I get a warning about the content headers being application/octet-stream

<NoVars: Not an HTML form submission (Content-Type: application/octet-stream)>

How do I implement a BlobStoreHandler in webapp2 outside of GAE?


Solution

  • I ended up using fineuploader http://fineuploader.com/ which sends a multipart encoded form to my handler endpoint.

    inside the handler I could simply just reference the POST and then read the FieldStorage Object into a cStringIO object.

    image = self.request.POST["qqfile"]
    imgObj = cStringIO.StringIO(image.file.read())
    
    # Connect to S3...
    # create s3 Key 
    key = bucket.new_key("%s" % uuid.uuid4());
    
    # guess mimetype for headers
    file_mime_type = mimetypes.guess_type(image.filename)
    if file_mime_type[0]:
       file_headers = {"Content-Type": "%s" % file_mime_type[0]}
    else:
       file_headers = None
    
    key.set_contents_from_string(imgObj.getvalue(), headers=file_headers)
    
    key_str = key.key
    
    #return JSON response with key and append to s3 url on front end.
    

    note: qqfile is the parameter fineuploader uses.

    I am faking the progress but that is ok for my use case no need for BlobStoreUploadHandler.