1

我对 Flash 和 AS3 完全陌生。我在谷歌上搜索了关于这个主题的任何内容。

我有一些将图像发布到 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");

问题是,为了进行实际发布,我必须导航到实际的 url(见最后一行代码)......

我希望能够将数据发布到 url 而无需导航到它。有任何想法吗?

4

1 回答 1

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;

我想我都得到了:)

于 2012-06-06T20:04:59.997 回答