首頁 > FLEX, 程式設計 > 使用 AMFPHP 1.9 上傳 bytearray 檔案,不需要使用 FileReference

使用 AMFPHP 1.9 上傳 bytearray 檔案,不需要使用 FileReference

2009年9月15日  瀏覽次數 : 8,719

通常我們要使用 FLEX 上傳檔案,我們會用 URLRequest 的 POST 方式,或者是使用 FileReference 來做上傳的動作,但是有時候總會有一些不方便,例如 URLRequest 他會開啟一個視窗,這樣如果被關掉就慘了,那如果用 FileReference.upload 又只能處理一個檔案,或是不可以處理 ByteArray 的資料,不過 AMFPHP 1.9 就支援使用 ByteArray 的資料格式囉。

image 圖 : 使用 remoting 來上傳檔案。

當然這個做法也不是全然都是好的,因為這種方式要預先載入 ByteArray ,所以一定要使用 FLASH PLAYER 10 的 CLIENT 喔,當然這也是一種冒險啦。先來看看結果吧。PS.這是一個假的範例,因為我的空間可不夠放檔案阿。

showExample1111[1]

其實如果只是檔案選擇後上傳,這倒是還好不一定需要用到,如果是描繪 FLEX 的圖案要上傳,就會使用到囉。另外不要忘記怎麼使用 amfphp 喔~~ 可以參考之前的文章。

來看看程式碼吧

amfupload.mxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" fontSize="16" xmlns:net="flash.net.*">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;
 
			//選擇檔案後先在 LOCAL 作讀取動作 *注意一定要 FLASH PLAYER 10 以上
			private function selectHandler(event:Event):void{
				this.fr.load();
			}
			//讀取完成後把上傳檔案按鈕打開
			private function completeHandler(event:Event):void{
				this.uploadBtn.enabled = true ;
				this.filename.text = this.fr.name;
			}
			//建立一個 OBJECT 把資料內容放進去,並且利用 AMFPHP 上傳
			private function uploadHandler():void{
				var vo:Object = new Object();
				vo.fileName = this.fr.name;
				vo.fileData = this.fr.data;
				this.remoting.upload(vo);
				this.progressbar.visible = true ;
			}
			//存等成功資訊
			private function resultHandler(event:ResultEvent):void{
				if(event.result.success == true){
					Alert.show("上傳成功!,存檔完成");
					this.progressbar.visible = false ;
				}
			}
		]]>
	</mx:Script>
 
	<mx:Label text="corAusir 範例 - 使用 AMFPHP 上傳檔案存檔"/>
	<mx:HBox width="330">
		<mx:TextInput id="filename" text="請點擊選擇檔案..." width="100%" editable="false"/>
		<mx:Button label="瀏覽檔案" cornerRadius="0" buttonMode="true" click="this.uploadBtn.enabled=false;this.fr.browse();"/>
	</mx:HBox>
	<mx:HBox width="330">
		<mx:ProgressBar width="100%" id="progressbar" indeterminate="true" visible="false"/>
		<mx:Button id="uploadBtn" enabled="false" label="上傳檔案" click="uploadHandler()" cornerRadius="0" buttonMode="true"/>
	</mx:HBox>
 
	<net:FileReference id="fr" select="selectHandler(event);" complete="completeHandler(event)"/>
	<mx:RemoteObject id="remoting" destination="amfphp" source="amfupload.fileupload" showBusyCursor="true" result="resultHandler(event)"/>
</mx:Application>

fileupload.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
define("UPLOADS_DIR","");
 
class fileupload{
      public function upload($vo){
            try{
                  file_put_contents ($vo['fileName'],$vo['fileData']->data);
 
                  //unlink(UPLOADS_DIR.$vo['fileName']);
                  $obj['success'] = true;
            }catch(Exception $e){
                  $obj['success'] = false;
                  $obj['error'] = $e->getMessage();
            }
 
 
            return $obj;
      }
}
?>

Random Posts

Loading…

:: 把這篇好文推到書籤網站與更多人分享吧 ::
  • funp
  • Hemidemi
  • YahooKimo
  • Google
  • udn
  • Haohao
  • Live

相關文章 :

Ausir FLEX, 程式設計 , , , ,

  1. forgot
    2009年10月23日21:06 | #1

    我在FLEX中編譯this.fr.load();時出現了
    1061: 呼叫可能未定義的方法 load (透過靜態類型 flash.net:FileReference 的參考)。
    這是??

  2. forgot
    2009年10月24日11:45 | #2

    @forgot
    原來是要在FlexCompiler裡面把Flash的版本設為10以上才可以

  3. forgot
    2009年10月24日12:25 | #3

    又有問題啦 這只可以上傳圖檔?
    是否有可以上傳檔案的?
    像是.DAE檔,文件檔之類的

  4. forgot
    2009年10月24日13:46 | #4

    @forgot
    怎麼一直在自問自答 ==!!
    可以傳其他類型的檔案,只是不能太大

  5. 2009年10月24日17:14 | #5

    @forgot

    抱歉啦~ 最近比較忙 回的比較慢
    關於第一個問題~ 沒錯~ 一定要 FLASH PLAYER 10.0.0 以上才可以喔~

    第二個問題是 可以 LOAD 沒問題
    但是你要先上傳 SERVER 然後再用 PV3D 讀出網路上的 DAE
    或是你要直接讀出資料塞進去應該也是可以~ 反正都是文字檔

  6. forgot
    2009年10月26日13:34 | #6

    我將localhost改成我的IP(固定的)
    原本的是http://localhost/AA-debug/AA.html
    改成http://我的IP/AA-debug/AA.html
    就無法上傳了 請問這是?

  7. forgot
    2009年10月26日18:20 | #7

    @forgot
    剛剛在跟組羊研究
    首先 把.xml檔裡的gateway改成你的IP
    接著 再去FlexServe裡面改url(好像可以不做)
    最後 也是最重要的一點 去走動走動 休息一下
    (跟組羊在那邊研究半天研究不出來
    最後去上個廁所回來就可跑,太誇張)

  8. 2009年10月31日10:01 | #8

    @forgot

    -.- 永遠在鬼打牆的迴圈內~~

    WHILE(鬼打牆){
    春青與熱情 -= 10 ;
    }

  9. 2010年1月23日01:13 | #9

    你好,我想請教一下,我已經將flash player安裝為10了
    但還是會出現
    1061: 呼叫可能未定義的方法 load (透過靜態類型flash.net:FileReference 的參考)。
    這樣的狀況該如何排除呢?

  10. 2010年1月23日09:29 | #10

    @兔

    hi 兔

    你必須要在 flex 專案的 compiler 的地方強制設定
    flashplayer 必須要在 10.0.0 以上喔

  11. 2010年5月3日01:23 | #11

    不過這樣就不能秀出上傳百分比進度了….

  12. 2010年7月27日15:59 | #12

    不好意思,想請問一下我照飯力打了以後出現這些錯誤
    能幫我看一下有哪些地方需要更改的嘛??
    謝謝你!
    [RPC Fault faultString="[MessagingError message='Destination 'amfphp' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']”

    faultCode=”InvokeFailed” faultDetail=”Couldn’t establish a connection to ‘amfphp’”]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::invoke()[C:\autobuild\3.5.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:333]
    at mx.rpc.remoting::Operation/http://www.adobe.com/2006/flex/mx/internal::invoke()[C:\autobuild\3.5.0\frameworks\projects\rpc\src\mx\rpc\remoting\Operation.as:247]
    at mx.rpc.remoting::Operation/send()[C:\autobuild\3.5.0\frameworks\projects\rpc\src\mx\rpc\remoting\Operation.as:219]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.rpc::AbstractService/http://www.adobe.com/2006/actionscript/flash/proxy::callProperty()[C:\autobuild\3.5.0\frameworks\projects\rpc\src\mx\rpc\AbstractService.as:353]
    at amfupload/uploadHandler()[C:\Users\spahappy\Adobe Flash Builder 4\amfupload\src\amfupload.mxml:22]
    at amfupload/__uploadBtn_click()[C:\Users\spahappy\Adobe Flash Builder 4\amfupload\src\amfupload.mxml:42]

  1. 本篇文章目前尚無任何 trackbacks 和 pingbacks。