views:

438

answers:

1

Hi, Anyone knows how to combine flex and django on upload images? I used fileRefenrece in Flex but I don't know how to relat it with a View in Django.

Thanks!

[EDIT] I'm using flex and django to make a site where people have an login e some data, like an mini an intern orkut. In the server-side i build my views, models etc, in the client-side, my design in flex and functions and events etc, calling the django using HTTPService with a specific url and view. So I click on a button, for exemple, call an HttpService myHttpService.send(), give the necessary parameters to my view and get an returned xml_response from the server and show it in the screen. But I want to allow logged people to change their onw profile photos(today in a folder on the server, and they have to send me a photo by email and a change manually when they want to change =/ ). I,ve tried to use FileReference and call a django view/url, but i don't know how to do that. I don't find nothing about that on internet, so if anybody knows how to make photos/files upload using django and flex, no matters how, will helps a lot!

Thanks and sorry for my bad english.

Exemple to logout(the id's and names are in portuguese):

my view (main.py):

@xml_view
def login(request, xml_resposta, xml_erros):
    params = request.POST or request.GET
    nome_usuario = params.get('usuario')
    senha = params.get('senha')
    sessao = Sessao.cria_autenticado(nome_usuario, senha)
    xml_resposta.addChild(xml.sessao_id(sessao.get_id()))
    return xml_resposta

url.py:

url(r'^logout/$', 'main.logout'),

my httpservice on flex:

<mx:HTTPService id="logoutRequest" url="/logout/" resultFormat="e4x" method="POST">
    <mx:request xmlns="">
        <sessao{parentApplication.getIdSessao()}/sessao>
    </mx:request>

</mx:HTTPService>

When I click on the Logout button I call logoutRequest.send()

+1  A: 

Flex FileReference objects post files to any URL in such a way that makes it look like a post from a normal form - so you can just use Django's normal file upload functions to handle uploads from Flex. The following is naive in many respects but illustrates the basic plumbing:

Flex:

        private var fileRef:FileReference;

  private function uploadFile():void
  {
   fileRef = new FileReference();
   fileRef.browse();
   fileRef.addEventListener(Event.SELECT, postFile);
   fileRef.addEventListener(ProgressEvent.PROGRESS, uploadProgress);
            fileRef.addEventListener(Event.COMPLETE, uploadComplete);
            fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioError);
  }

  private function postFile(event:Event):void
  {

   var req:URLRequest = new URLRequest('http://yourserver/yourapp/upload');
   fileRef.upload(req, 'from_flex'); // 'from_flex' is the key you use to refer to the file in django's request.FILES dict
  }

  private function uploadProgress(event:ProgressEvent):void
  {
   trace('progress');
  }

  private function uploadComplete(event:Event):void
  {
   trace('done');
  }

  private function ioError(event:IOErrorEvent):void
  {
   trace('error: '+event.toString());
  }

In Django you just need a view that receives the file and does whatever it is you want:

def receive_file(request):
    received_file = request.FILES['from_flex']
    destination = open('recieved_file.jpg', 'wb+')
    # naively save the file to disk in this case:
    file_data = received_file.read()
    destination.write(file_data)
    destination.close()
    return HttpResponse()

You will also need to add an entry to your urls.py file to expose the above view via some url. See the django docs for more information about dealing with uploaded files:

stephen