Flex 上传文件,并提前浏览图片

21 篇文章 0 订阅
<?xml version="1.0" encoding="utf-8"?>
<s:Application  xmlns:fx="http://ns.adobe.com/mxml/2009" 
				xmlns:s="library://ns.adobe.com/flex/spark" 
				xmlns:mx="library://ns.adobe.com/flex/mx">	
	<fx:Style>
		global {
			fontSize : 12;
		}
	</fx:Style>
	
	<fx:Script>
		<![CDATA[
			// 先搞 1 个 FileReference
			private var file:FileReference = new FileReference();
			
			// 上传状态指示, 和下面的文本框绑定
			[Bindable]
			private var stateText:String = "请选择一个文件上传";
			
			// createChildren 比 creationComplete 事件更早发生, 省的注册事件侦听, 直接在这里写了
			protected override function createChildren():void {
				super.createChildren();
				file.addEventListener(Event.SELECT, file_select);
				file.addEventListener(Event.COMPLETE, onLoadComplete);
				file.addEventListener(ProgressEvent.PROGRESS, file_progress);
				file.addEventListener(Event.COMPLETE, file_complete);
			}
			
			// 选择 1 个文件的事件
			private function file_select (e:Event):void {
				stateText = "选择了文件 " + file.name;
				file.load();
			}
			
			// 上传完毕后的事件
			private function file_complete (e:Event):void {
				stateText = "上传完毕";
			}
			
			private function file_progress (e:ProgressEvent):void {
				stateText = "已上传 " + Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";
			}
			// 先判断一下文件大小, 再上传, FileService.aspx 就是上传地址
			private function upload():void {
				if (file.size > 0) {
					stateText = "正在上传 " + file.name;
					var request:URLRequest = new URLRequest("http://10.19.1.55/FileService/FileService.aspx");
					file.upload(request);
				}
			}
			private function onLoadComplete(e:Event):void				
			{						
				img.source = file.data;				
			}
			
			
		]]>
	</fx:Script>
	<s:Image id="img" width="100%" height="100%"></s:Image>
	<s:Panel width="250" height="112" title="上传示例" horizontalCenter="0" verticalCenter="0">
		<s:VGroup>
			<mx:HBox>
				<mx:TextInput text="{stateText}" width="160" editable="false"/>
				<mx:Button label="浏览" click="file.browse();"/>
			</mx:HBox>
			<mx:HBox>
				<mx:Button label="上传" click="upload();"/>
			</mx:HBox>
		</s:VGroup>		
	</s:Panel>
</s:Application>


FileService.aspx服务器端代码:

string uploadFolder = "upload"; // 上传文件夹
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection files = Request.Files;

        if (files.Count == 0)
        {
            Response.Write("请勿直接访问本文件");
            Response.End();
        }

        string path = Server.MapPath(uploadFolder);

        // 只取第 1 个文件
        HttpPostedFile file = files[0];

        if (file != null && file.ContentLength > 0)
        {
            // flash 会自动发送文件名到 Request.Form["fileName"]
            string savePath = path + "/" + Request.Form["fileName"];
            file.SaveAs(savePath);
        }
    }

配置文件添加:

<configuration>
	<system.web>
		<httpRuntime maxRequestLength="4096" executionTimeout="10000" />
	</system.web>
</configuration>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值