将数据发布到网址而不导航到它?

时间:2012-06-06 19:44:33

标签: php actionscript-3 flash post httpwebrequest

我是Flash和AS3的新手。我用Google搜索了一下这个主题无法找到任何内容。

我有一些代码将图片发布到php文件:

var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
    var jpgURLRequest:URLRequest = new URLRequest("http://127.0.0.1/gdipORG/takeImage.php");
    jpgURLRequest.requestHeaders.push(header);
    jpgURLRequest.method = URLRequestMethod.POST;
    jpgURLRequest.data = byteArray;
navigateToURL(jpgURLRequest, "blank");

问题是,对于实际发布的帖子,我必须导航到实际的网址(参见最后一行代码)...

我希望能够将数据发布到网址,而无需导航到它。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在现有代码后面使用URLLoader:

//Existing code
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("http://127.0.0.1/gdipORG/takeImage.php");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = byteArray;

//KILL THIS LINE navigateToURL(jpgURLRequest, "blank");

//Add these (UNTESTED CODE):

var sendJPGLoader:URLLoader = new URLLoader();
sendJPGLoader.dataFormat = URLLoaderDataFormat.BINARY;
sendJPGLoader.addEventListener(Event.COMPLETE, sendJPGToServerComplete);
sendJPGLoader.addEventListener(IOErrorEvent.IO_ERROR, sendJPGToServerIOError);

//Try to send image
sendJPGLoader.load(jpgURLRequest); 

function sendJPGToServerComplete(evt:Event):void {
  //Request was sent to server succesfully
  //Optionally check server response
  // var serverResponse:String = String(evt.target.data);
}

function sendJPGToServerIOError(evt:Event):void {
  //Failed
}

在代码顶部不要忘记导入:

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;

我想我得到了所有这些:)