flex结合django上传图片,之前在网上只找到一篇文章http://blog.douhua.im/2009/06/14/flex-django-upload/
自己在此基础上进行,最终实现其功能,当成功之后,前台得到返回的上传图片名称。
flex端的mxml文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationPolicy="auto"
layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import com.adobe.cairngorm.model.ModelLocator;
import mx.events.CloseEvent;
import mx.controls.Alert;
import mx.managers.CursorManager;
private var file:FileReference = new FileReference();
private var _upload:URLRequest;
[Bindable]
private var percent : int = 0;
public function init():void
{
file.addEventListener(Event.SELECT, selectHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
browseBtn.addEventListener(MouseEvent.CLICK, browseHandler);
}
public function browseHandler(event:MouseEvent): void
{
file.browse();
}
public function reset(): void
{
fileTxt.text="";
processBar.setProgress(0, 100);
processBar.visible=false;
processBar.label="Uploading " + 0 + "%";
}
public function selectHandler(event:Event): void
{
fileTxt.text=file.name;
}
private function ioErrorHandler(event:Event):void{
Alert.show("fail");
}
public function uploadCompleteDataHandler(event:DataEvent): void{
tt.text=event.data;
Alert.show("success");
}
public function progressHandler(event:ProgressEvent): void
{
var size:int = file.size;
percent = event.bytesLoaded/event.bytesTotal * 100;
processBar.setProgress(percent, 100);
processBar.label="Uploading " + percent + "%";
if (event.bytesLoaded == event.bytesTotal)
{
processBar.label="Upload file successfully!";
}
}
public function upload(): void
{
if(fileTxt.text == "")
{
Alert.show("Please select upload file");
return;
}
_upload = new URLRequest('http://localhost:8000/upload/');
_upload.method = URLRequestMethod.POST;
_upload.contentType = "multipart/form-data";
processBar.visible=true;
file.upload(_upload,'file',true);
}
]]>
</mx:Script>
<mx:Panel width="428" height="256" layout="absolute" title="File Upload" fontSize="12" y="58" x="59">
<mx:Label x="49" y="32" text="File:"/>
<mx:TextInput x="88" y="30" id="fileTxt" width="226" editable="false"/>
<mx:Button id="browseBtn" x="316.5" y="30" label="打开"/>
<mx:Button id="uploadBtn" x="88" y="92" label="上传" click="upload()"/>
<mx:Button id="cancelBtn" x="183" y="92" label="Cancel" visible="false"/>
<mx:ProgressBar id="processBar" labelPlacement="bottom" themeColor="#EE1122" minimum="0"
visible="false" maximum="100" color="0x323232" label="Loading 0%"
direction="right" mode="manual" width="358.5" y="144" x="21.5"/>
<mx:Label x="49" y="150" text="Label" id="tt"/>
</mx:Panel>
</mx:Application>
django端的处理文件
from django import forms
from django.http import HttpResponse
def upload(request):
UPLOAD_PATH = 'flex/assets/pic/'
if request.method == 'POST':
img = request.FILES['file']
destination = open(UPLOAD_PATH + img.name, 'wb+')
for chunk in img.chunks():
destination.write(chunk)
destination.close()
return HttpResponse(img.name)
别忘了配置setting.py,urls.py
over
如果有需要,再整理上传源码文件。
本文介绍如何使用Flex和Django实现图片上传功能。详细展示了Flex端MXML代码及Django后端处理流程,包括文件选择、上传进度显示、上传完成提示等功能。
874

被折叠的 条评论
为什么被折叠?



