AS3 - 取消PHP请求

时间:2011-10-01 22:26:16

标签: php actionscript-3 upload

下面的脚本用于通过PHP上传图像。现在我想给用户提供取消上传的选项。 如何在发送后取消PHP请求?

    package
{
    import com.adobe.images.PNGEncoder;
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.SecurityErrorEvent;
    import flash.events.IOErrorEvent
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.utils.ByteArray;

    public class ServeruploadExample
    {
        private var loader:URLLoader;

        private const API_KEY:String = "<api key>";
        private const UPLOAD_URL:String = "http://example.com/upload-image.php";

        public function ImgurExample() {

            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader.addEventListener(Event.COMPLETE, onCookieSent);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
            loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

            // Create a bitmapdata instance of a movieclip on the stage.
            var mc:MovieClip;
            var b:BitmapData = new BitmapData(mc.width, mc.height, true);
            b.draw(mc);
            var png:ByteArray = PNGEncoder.encode(b);

            var vars:String = "?key=" + API_KEY + "&name=name&title=title";
            var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
            request.contentType = "application/octet-stream";
            request.method = URLRequestMethod.POST;
            request.data = png;

            loader.load(request);
        }
        // privates


    }
}

1 个答案:

答案 0 :(得分:1)

只需调用 URLloader 类的.close()方法即可终止加载操作。

在您的示例中,设置取消按钮并:

public function cancel_upload_click_handler(e:MouseEvent):void
{
   loader.close();
}