Skip to content

POSTing a ByteArray from Flash to Web.py

2010 August 16
by Tedb0t

…turns out to be really straightforward using the web.data() method (thanks to Brian for the heads-up!):

Flash codeā€”for this example you’ll need the AS3CoreLib JPGEncoder class:

_video = new Video(640, 480);
_camera = Camera.getCamera();
_camera.setMode(640, 480, 30);
_video.attachCamera(_camera);
addChild(_video);

_jpegEncoder = new JPGEncoder(60);

_urlReq = new URLRequest();
_urlReq.url = "http://localhost:8080/addImage";
_urlReq.method = URLRequestMethod.POST;

_urlLoader = new URLLoader();

_snapShot = new BitmapData(_camera.width, _camera.height);
_snapShot.draw(_video);
_snapShotImage = new Bitmap(_snapShot);
var jpgStream:ByteArray = _jpegEncoder.encode(_snapShot);
_urlReq.data = jpgStream;

try {
	_urlLoader.load(_urlReq);
} catch (e:Error) {
	trace("Remote.request caught error: "+e);
}

And the Python (using web.py):

import web

urls = (
  '/addImage', 'addImage'
)

app = web.application(urls, globals())

class addImage:
    def POST(self):
        data = web.data()
        f = open("test.jpg", 'wb')
        f.write(data)

if __name__ == "__main__": app.run()

That’s it!

Related Posts:


No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS