2009-11-21 113 views
0

任何人都知道如何在上傳圖像上結合使用flex和django? 我在Flex中使用了fileRefenrece,但我不知道如何使用Django中的View進行關聯。使用flex和django上傳圖像

謝謝!

我正在使用flex和django製作一個網站,其中有人登錄了一些數據,比如一個迷你實習生orkut。在服務器端,我構建了我的視圖,模型等,在客戶端,我的flex和函數和事件等的設計中,使用具有特定url和視圖的HTTPService調用django。 所以我點擊一個按鈕,例如,調用HttpService myHttpService.send(),給我的視圖提供必要的參數,並從服務器獲取返回的xml_response並將其顯示在屏幕中。 但我想允許登錄人員更改他們的個人資料照片(今天在服務器上的一個文件夾中,他們必須通過電子郵件向我發送照片,並在他們想要更改時手動進行更改= /)。 我試圖使用FileReference並調用django視圖/ url,但我不知道該怎麼做。我在互聯網上沒有發現任何信息,所以如果有人知道如何使用django和flex上傳照片/文件,不管怎麼樣,都會有很大的幫助!

感謝和抱歉我的英語不好。

爲例註銷(ID用和名稱是葡萄牙語):

我的觀點(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'), 

我的HTTPService上彎曲:

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

</mx:HTTPService> 

當我CL我可以通過LogoutRequest.send()登錄LogoutRequest.send()

+0

含糊不清的問題:請充實。這是兩種完全不同的技術,所以請告訴我們他們是如何連接的,您的請求如何進行等等。 – jkp 2009-11-21 15:46:30

回答

1

Flex FileReference對象將文件發佈到任何URL,使得它看起來像來自普通表單的文章 - 所以您可以使用Django的普通文件上傳功能處理來自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()); 
} 

在Django中,你只需要接收文件並執行不管它是你想有一個觀點:

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() 

您還需要在urls.py文件中添加一個條目,以便通過某個url顯示上述視圖。有關處理上傳文件的更多信息,請參閱django文檔: