flex調用php上傳圖片

本例實現點擊按鈕彈出上傳提示框, 調用php文件, 將圖片上傳至指定位置

main.mxml

<?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:Script>
		<![CDATA[
		import mx.managers.PopUpManager;
		
		protected function button1_clickHandler(event:MouseEvent):void
			{
				var imageWindow:ImageWindow = ImageWindow(PopUpManager.createPopUp(this, ImageWindow, false));
				PopUpManager.centerPopUp(imageWindow);
			}
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 將非可視元素(例如服務、值對象)放在此處 -->
	</fx:Declarations>
	<s:Button x="183" y="228" label="彈出" click="button1_clickHandler(event)"/>
</s:Application>
ImageWindow.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   width="700" height="350" title="設置圖片"
			   close="PopUpManager.removePopUp(this);">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.managers.CursorManager;
			import mx.managers.PopUpManager;
			import mx.rpc.events.ResultEvent;
			
			[Bindable]
			private var file:FileReference = new FileReference();

			protected function chooseFile(event:MouseEvent):void
			{
				var imgFileFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
				file.browse([imgFileFilter]);
				file.addEventListener(Event.SELECT, haveSelected);
			}

			private function haveSelected(event:Event):void
			{
				if(FileReference(event.target).size > 512000)
				{
					Alert.show("文件大小超過500K, 請重新選擇!", "提示");
					return;
				}
				fileName.text = file.name;
			}

			protected function upLoadFile(event:MouseEvent):void
			{
				if(fileName.text == "未選擇文件")
				{
					Alert.show("請選擇要上傳的文件", "提示");
					return;
				}
				else
				{
					var request:URLRequest = new URLRequest("http://localhost:8008/uploadfiles.php");
					file.addEventListener(Event.COMPLETE, upLoadFileComplete);
					file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
					pb.visible = true;
					file.upload(request);
				}
			}

			private function progressHandler(event:ProgressEvent):void
			{
				pb.setProgress(event.bytesLoaded, event.bytesTotal);
			}
			
			private function upLoadFileComplete(event:Event):void
			{
				PopUpManager.removePopUp(this);
				Alert.show("文件上傳成功!", "提示");
			}

			protected function stopUpLoad(event:MouseEvent):void
			{
				file.cancel();
				PopUpManager.removePopUp(this);
			}

		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 將非可視元素(例如服務、值對象)放在此處 -->
	</fx:Declarations>
	<mx:ViewStack id="imageViewStack" cornerRadius="5" backgroundColor="#EFF1F2" borderStyle="solid" top="20" right="15" left="15" bottom="60">
		<s:NavigatorContent label="本地圖片" width="100%" height="100%">
			<s:VGroup width="100%" height="100%" paddingTop="30" paddingLeft="30" paddingRight="30" gap="10">
				<s:Label text="選擇您的圖片並上傳:" fontSize="15" width="100%" color="#777777"/>
				<s:HGroup width="100%" verticalAlign="middle" horizontalAlign="center" gap="20">
					<s:Button label="選擇" click="chooseFile(event)"/>
					<s:Label id="fileName" text="未選擇文件" fontSize="13" color="#777777"/>
					<mx:ProgressBar id="pb" mode="manual" labelPlacement="center" visible="false"/>
				</s:HGroup>
				<mx:Spacer height="5"/>
				<s:BorderContainer cornerRadius="5" width="100%" height="80" borderColor="#D7D7D7">
					<s:Label text="您可以選擇一張本地的圖片, 用來設置您的圖形的背景, 只允許上傳.jpg .png .pif格式的圖片, 文件最大500K." top="20" left="20" right="20" fontSize="15" color="#777777"/>
				</s:BorderContainer>
			</s:VGroup>
		</s:NavigatorContent>
		<s:NavigatorContent label="搜索" width="100%" height="100%">
			
		</s:NavigatorContent>
		<s:NavigatorContent label="網絡圖片" width="100%" height="100%">
			
		</s:NavigatorContent>
	</mx:ViewStack>
	<s:TabBar dataProvider="{imageViewStack}" left="25" top="10" cornerRadius="5" chromeColor="#6D93FF" focusColor="#70EE88"/>
	<s:Button y="275" label="確定" right="100" click="upLoadFile(event)" chromeColor="#8FC3F3"/>
	<s:Button y="275" label="取消" right="16" chromeColor="#8FC3F3" click="stopUpLoad(event)"/>
</s:TitleWindow>

uploadfiles.php

<?php
	$fileName = $_FILES["Filedata"]["name"];
	$file = $_FILES["Filedata"]["tmp_name"];     
	$path = "uploadFiles/";     
	if (move_uploaded_file($file, $path . $fileName)){     
		echo 1;     
	}else{     
		echo 0;     
	} 
?>
注:想看見所選的上傳圖片應在www文件夾下創建一個uploadFiles文件夾用來存儲上傳圖片

發佈了29 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章