2012-03-04 69 views
6

我有一個關於ActionScript 3中的POST文件上傳的快速問題。我試圖通過POST上傳一個ByteArray通過POST到服務器。我使用URLRequest類來發送數據,而URLLoader是因爲我想監視進度。代碼的有關章節如下:POST文件上傳使用URLRequest

var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php"); 
uploadRequest.method = URLRequestMethod.POST; 
uploadRequest.contentType = "multipart/form-data"; 
uploadRequest.data = myByteArray; 

var uploader:URLLoader = new URLLoader; 
uploader.addEventListener(ProgressEvent.PROGRESS, onUploadProgress); 
uploader.addEventListener(Event.COMPLETE, onUploadComplete); 
uploader.dataFormat = URLLoaderDataFormat.BINARY; 
uploader.load(uploadRequest); 

的問題是,我已經把我的回調,以跟蹤上傳進度,以及ProgressEvent的bytesTotal屬性始終是1960年(請求減去數據的大小?)即使實際數據大約爲20MB,即使Complete事件觸發後也沒有文件上傳。

我已驗證upload.php的功能是否正確,使用簡單的html表單,我可以驗證myByteArray是否包含所有有問題的數據。誰能告訴我我做錯了什麼?

編輯:

我已經嘗試了幾個,我想我應該提到新的東西。首先是將內容類型設置爲application/octet-stream而不是multipart/form-data,除了將字節數增加到1964之外,它沒有任何效果。我還檢查了Apache錯誤日誌,發現以下文本重複了很多:

PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

我猜測Flash並沒有正確地格式化HTTP請求,無論出於何種原因。鑑於我創建了一個FileReference,它使用我爲URLLoader設置的相同方法從磁盤上載文件,並獲得了預期結果:bytesTotal屬性與文件大小匹配並且文件正確上傳。

從我在提到使用調用FileReference.upload數據上傳到服務器Adobe開發文檔發現了一個工作頁面()通過設置URLRequest的數據參數,所以我嘗試下面的代碼:

var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php"); 
uploadRequest.method = URLRequestMethod.POST; 
uploadRequest.data = myByteArray; 

fileRef = new FileReference; 
fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress); 
fileRef.addEventListener(Event.COMPLETE, onUploadComplete); 
fileRef.upload(uploadRequest); 

這就造成了以下的輸出:

ArgumentError: Error #2127: FileReference POST data cannot be type ByteArray.

我真的被困在這裏。任何建議,將不勝感激!

+0

你試過 - uploadRequest.contentType = 「應用程序/八位字節流」; – Neil 2012-03-05 15:32:35

+0

同樣的結果,唯一的區別是bytesTotal是1964(四個額外的字符)。儘管謝謝你的建議! – 2012-03-05 21:06:45

+0

對有興趣的人進行快速更新。我檢查了Apache錯誤日誌,並試圖將文件結果上傳到這一行:「PHP警告:缺少第0行未知的多部分/表單數據POST數據中的邊界」 – 2012-03-05 21:44:36

回答

8

您應該添加更多的信息到 「內容類型」 標頭:

uploadRequest.contentType = "multipart/form-data; boundary=<<boundary here>>"; 

入住這裏的例子:http://marstonstudio.com/?p=36

+0

工作正常!非常感謝!我認爲Flash會爲我添加邊界。難怪它沒有工作。 – 2012-03-07 00:41:08

+0

很高興聽到它。 :) – deadrunk 2012-03-07 08:00:07

+3

鏈接已損壞 – Soid 2015-04-15 07:34:36