flex on django上传文件

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

如果有需要,再整理上传源码文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值