AS3 / AIR:尝试上载文件时出现IOStream错误

时间:2012-01-26 02:46:27

标签: actionscript-3 file-upload air drag-and-drop iostream

我正在创建一个AIR应用程序,用户将图像文件拖放到舞台上,然后应用程序将这些已删除的图像上传到网站imgur.com,然后返回图像链接(XML格式) 。

我有两个问题:

  1. 程序使用一个非常小的文件成功运行,但是任何更大的(1mb或更多),我得到一个IOStream错误2023.Imgur支持高达10mb的上传,所以我知道它必须是我的结果。

  2. 我希望能够将多个图像文件一次性放入程序中,并将其全部上传(同步或异步,无关紧要)。现在,当我尝试执行此操作时,程序会出错(类似于byteArray的行必须大于0)。

  3. 以下是有问题的代码,它给了我一些问题。 (谢谢你的高级帮助)

    private function doDragDrop(e:NativeDragEvent):void
    {           
        trace("doDragDrop() called.");
        var dropFiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
        for each (var file:File in dropFiles)
        {
            switch (file.extension.toLowerCase())
            {
                case "jpg" :
                case "jpeg" :
                case "gif" :
                case "png" :
                case "apng" :
                case "tiff" :
                case "bmp" :
                case "pdf" :
                case "xcf" :
                trace("file Extension Check = passed");
                addImage(file.nativePath);
                    break;
    
                //error handling for non-supported filetypes
                default :
                    trace("ERROR: Unsupported File Type Detected!");
            }
        }
    }
    
    private function addImage(nativePath:String):void
    {
        trace("addImage() called.");
        trace("NativePath is: " + nativePath);
        //var file:File = new File(nativePath);
        var file:File = File.desktopDirectory.resolvePath(nativePath);
        var ba:ByteArray = new ByteArray();
        var stream:FileStream = new FileStream();
        stream.open(file, FileMode.READ);
        stream.readBytes(ba);
        stream.close();         
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
        loader.loadBytes(ba);
    }
    
    private function fileLoaded(e:Event):void
    {
        trace("fileLoaded() called.");
        var bitmap:Bitmap = Bitmap(e.target.content);
        var bitmapData:BitmapData = bitmap.bitmapData;
        var png:ByteArray = PNGEncoder.encode(bitmapData);
    
        urlLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
        urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
        urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
        urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    
        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;
        urlLoader.load(request);
    }
    
    private function onCookieSent(e:Event):void
    {
        trace("onCookieSent() called.");
        var res:XML = new XML(unescape(urlLoader.data));
        var resultsList:XMLList = res.links;
        trace(resultsList);
    }
    
    private function onIOError(e:IOErrorEvent):void 
    {
        trace("ioErrorHandler: " + e);
        TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
        // Handle error
    }
    private function onSecurityError(e:SecurityErrorEvent):void 
    {
        trace("securityErrorHandler: " + e);
        TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
        // handle error
    }
    
    //When the dragged object leaves the drop point, do this
    private function doDragExit(e:NativeDragEvent):void
    {
        trace("doDragExit() called.");
        TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
    }
    

1 个答案:

答案 0 :(得分:1)

这样做之后:

var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close(); 

您已经拥有ByteArray文件/图片。你不能发这个......

private function addImage(nativePath:String):void
{
    trace("addImage() called.");
    trace("NativePath is: " + nativePath);
    //var file:File = new File(nativePath);
    var file:File = File.desktopDirectory.resolvePath(nativePath);
    var ba:ByteArray = new ByteArray();
    var stream:FileStream = new FileStream();
    stream.open(file, FileMode.READ);
    stream.readBytes(ba);
    stream.close();         

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

    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 = ba;
    urlLoader.load(request);
}