Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had to tackle a similar problem (uploading single photo from Flex to Django) while working on captionmash.com, maybe it can help you. I was using PyAMF for normal messaging but FileReference class had a built in upload method, so I chose the easy way.</p> <p>Basically system allows you to upload a single file from Flex to Google App Engine, then it uses App Engine's Image API to create thumbnail and also convert image to JPEG, then upload it to S3 bucket. <a href="http://code.google.com/p/boto/" rel="nofollow">boto</a> library is used for Amazon S3 connection, you can view the whole code of the project <a href="https://github.com/ocanbascil/captionmash" rel="nofollow">here</a> on github.</p> <p>This code is for single file upload only, but you should be able to do multi-file uploads by creating an array of FileReference objects and calling upload method on all of them.</p> <p>The code I'm posting here is a bit cleaned up, if you still have problems you should check the repo out.</p> <p>Client Side (Flex):</p> <pre><code> private function upload(fileReference:FileReference, album_id:int, user_id:int):void{ try { //500 kb image size if(fileReference.size &gt; ApplicationConstants.IMAGE_SIZE_LIMIT){ trace("File too big"+fileReference.size); return; } fileReference.addEventListener(Event.COMPLETE,onComplete); var data:URLVariables = new URLVariables(); var request:URLRequest = new URLRequest(ApplicationConstants.DJANGO_UPLOAD_URL); request.method = URLRequestMethod.POST; request.data = data; fileReference.upload(request,"file"); //Popup indefinite progress bar } catch (err:Error) { trace("ERROR: zero-byte file"); } } //When upload complete private function onComplete(evt:Event):void{ fileReference.removeEventListener(Event.COMPLETE,onComplete); //Do other stuff (remove progress bar etc) } </code></pre> <p>Server side (Django on App Engine):</p> <p>Urls:</p> <pre><code>urlpatterns = patterns('', ... (r'^upload/$', receive_file), ... </code></pre> <p>Views:</p> <pre><code>def receive_file(request): uploadService = UploadService() file = request.FILES['file'] uploadService.receive_single_file(file) return HttpResponse() </code></pre> <p>UploadService class</p> <pre><code>import uuid from google.appengine.api import images from boto.s3.connection import S3Connection from boto.s3.key import Key import mimetypes import settings def receive_single_file(self,file): uuid_name = str(uuid.uuid4()) content = file.read() image_jpeg = self.create_jpeg(content) self.store_in_s3(uuid_name, image_jpeg) thumbnail = self.create_thumbnail(content) self.store_in_s3('tn_'+uuid_name, thumbnail) #Convert image to JPEG (also reduce size) def create_jpeg(self,content): img = images.Image(content) img_jpeg = images.resize(content,img.width,img.height,images.JPEG) return img_jpeg #Create thumbnail image using file def create_thumbnail(self,content): image = images.resize(content,THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,images.JPEG) return image def store_in_s3(self,filename,content): conn = S3Connection(settings.ACCESS_KEY, settings.PASS_KEY) b = conn.get_bucket(BUCKET_NAME) mime = mimetypes.guess_type(filename)[0] k = Key(b) k.key = filename k.set_metadata("Content-Type", mime) k.set_contents_from_string(content) k.set_acl("public-read") </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload