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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3 / AIR:尝试上载文件时出现IOStream错误相关的知识,希望对你有一定的参考价值。

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

我有两个问题:

  1. 该程序使用一个非常小的文件成功运行,但是任何更大的(1mb或更多),我得到一个iostream错误2023.Imgur支持高达10mb的上传,所以我知道它必须是我的结果。
  2. 我希望能够一次将多个图像文件放入程序中,并将其全部上传(同步或异步,无关紧要)。现在,当我尝试执行此操作时程序会出错(byteArray的行必须大于0)。

以下是有问题的代码,它给我提出了问题。 (谢谢你的高级帮助)

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});
}
答案

这样做之后:

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);
}

以上是关于AS3 / AIR:尝试上载文件时出现IOStream错误的主要内容,如果未能解决你的问题,请参考以下文章

尝试创建 YouTube API 的 Air Native Extension 时出现 ClassNotFoundException

Adobe Air iOs 包含“资源”文件夹时出现打包错误。为啥?

当安装INSTALL_FAILED_NO_MATCHING_ABIS时出现Android AIR 33错误:无法提取本机库,res = -113

添加按钮以将图片保存到AIR或AS3中

加载初始内容Adobe air时出现ADL错误

AS3 / AIR - 创建纯文本文件?