【问题标题】:Uploading image using flex and django使用 flex 和 django 上传图片
【发布时间】:2009-11-21 15:23:49
【问题描述】:

有人知道如何在上传图片时结合使用 flex 和 django 吗? 我在 Flex 中使用了 fileRefenrece,但我不知道如何将它与 Django 中的 View 关联起来。

谢谢!

[编辑] 我正在使用 flex 和 django 制作一个网站,人们可以在其中登录并获取一些数据,例如迷你实习生 orkut。在服务器端,我构建我的视图、模型等,在客户端,我在 flex 和函数和事件等方面的设计,使用具有特定 url 和视图的 HTTPService 调用 django。 因此,我单击一个按钮,例如,调用 HttpService myHttpService.send(),为我的视图提供必要的参数,并从服务器获取返回的 xml_response 并将其显示在屏幕上。 但我想允许登录的人更改他们的 onw 个人资料照片(今天在服务器上的一个文件夹中,他们必须通过电子邮件向我发送一张照片,并在他们想要更改时手动更改 =/)。 我尝试使用 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'),

我在 flex 上的 httpservice:

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

</mx:HTTPService>

当我点击注销按钮时,我调用 logoutRequest.send()

【问题讨论】:

  • 非常模糊的问题:请充实。这是两种完全不同的技术,所以请告诉我们它们是如何连接的,您的请求是如何提出的等等。

标签: django apache-flex upload


【解决方案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 文档:

【讨论】:

    猜你喜欢
    • 2011-11-07
    • 2013-08-17
    • 1970-01-01
    • 2016-02-05
    • 2014-07-01
    • 2011-07-24
    • 2021-12-19
    • 1970-01-01
    • 2015-08-06
    相关资源
    最近更新 更多