开发者

How to upload files in flex using PyAMF or PhpAMF? client side, and very little server side help needed

Hy!

  1. I need to upload a group of images using flex with robotlegs.
  2. I need a progress bar to work when image is uploading.
  3. It might upload 1 image or more at the time.
  4. I want to know if uploading byteArray to server and then save the image is too heavy for the server.
  5. In the server side I have a method that is made by pyamf, and looks like this:

.

def upload_image(input):
    # here does stuff. I need to be abl开发者_运维知识库e to get parametters like this
    input.list_key
    # and here I need some help on how to save the file

Thanks ;)


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.

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. boto library is used for Amazon S3 connection, you can view the whole code of the project here on github.

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.

The code I'm posting here is a bit cleaned up, if you still have problems you should check the repo out.

Client Side (Flex):

    private function upload(fileReference:FileReference,
                            album_id:int,
                            user_id:int):void{
        try {
            //500 kb image size
            if(fileReference.size > 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)
    }

Server side (Django on App Engine):

Urls:

urlpatterns = patterns('',
    ...
    (r'^upload/$', receive_file),    
    ...

Views:

def receive_file(request):
    uploadService = UploadService()
    file     = request.FILES['file']
    uploadService.receive_single_file(file)
    return HttpResponse()

UploadService class

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")  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜