AS3从服务器中的SWF浏览客户端文件

时间:2012-09-23 16:27:23

标签: actionscript-3 filereference

这是AS3上的新手! :)

基本上我正在尝试编写一个应用程序,让用户选择一个图像文件并显示它(然后我将操纵像素,所以我不希望应用程序将图像存储在一个新文件中,只是管理字节阵列)。

到目前为止,我在Flash Develop中编写了一些工作代码,显示了一个窗口来选择图像,然后显示它。但是当我将生成的文件(myapplication.swf,expressinstall.swf,index.html和js文件夹)上传到服务器时,窗口不再显示。

我正在使用FileReference.browse()方法。

怎么了?

(编辑:正如The_asMan在这里指出的那样,我们错过了一些代码,这里是 - 根据The_asMan的建议进行了改进)

我的包裹:

package searchfiles 
{
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.net.FileReference;
    import flash.net.FileReferenceList;
    import flash.net.FileFilter;
    import flash.events.*;
    import flash.net.FileFilter; 
    import flash.net.FileReference; 
    import flash.net.URLRequest; 
    import flash.utils.ByteArray; 
    import flash.display.DisplayObject;

    /**
     * ...
     * @author ddd
     */
    public class searchForFiles extends EventDispatcher
    {
        public var newfile:FileReference;
        public var loader:Loader
        public var bitmapimg:BitmapData;            

        public function searchForFiles() {
            newfile = new FileReference();
            newfile.addEventListener(Event.SELECT, onFileSelected); 
            newfile.addEventListener(Event.CANCEL, onCancel); 
            newfile.addEventListener(IOErrorEvent.IO_ERROR, onIOError); 
            newfile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); 

            trace("abbiamo instanziato un searchForFiles");
            var textTypeFilter:FileFilter = new FileFilter("Image files (*.png, *.jpg, *tif)", 
                        "*.png; *.jpg; *tif"); 
            newfile.browse([textTypeFilter]);   

        }       

        public function onFileSelected(evt:Event):void 
        { 
            newfile.addEventListener(ProgressEvent.PROGRESS, onProgress); 
            newfile.addEventListener(Event.COMPLETE, onComplete); 
            newfile.load(); 
        } 

        public function onProgress(evt:ProgressEvent):void 
        { 
            trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes."); 

        } 

        public function onComplete(evt:Event):void 
        { 
            trace("File was successfully loaded."); 
            loader = new Loader();              
            loader.loadBytes(newfile.data);         
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);           
        } 

        private function erroremanip(evt:IOErrorEvent):void {
            trace("errore " + evt);
        }
        private var bitmapData:BitmapData

        public function getBitmapData(e:Event):void {
            var content:* = loader.content;
            bitmapData = new BitmapData(content.width,content.height,true,0x00000000);
            trace("loader.width = " +loader.width);
            dispatchEvent( new Event(Event.COMPLETE));
            //trace("get bitmap data called");
        }

        public function onCancel(evt:Event):void 
        { 
            trace("The browse request was canceled by the user."); 
        } 

        public function onIOError(evt:IOErrorEvent):void 
        { 
            trace("There was an IO Error."); 
        } 
        public function onSecurityError(evt:Event):void 
        { 
            trace("There was a security error."); 
        }                       
    }    
}

这里是main()

package 
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.errors.IOError;
    import flash.events.*;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import searchfiles.searchForFiles;

    /**
     * ...
     * @author ddd
     */
    [SWF(width = "550", height = "600")]

    public class Main extends MovieClip 
    {
        public var file:searchForFiles;
        public var mybtn:Loader = new Loader();

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point          
            mybtn.addEventListener(MouseEvent.CLICK, mouseclicked);
            mybtn.addEventListener(IOErrorEvent.IO_ERROR, erroremanip);
            var urlqst:URLRequest = new URLRequest("preview_true.png");
            mybtn.load(urlqst);
            addChild(mybtn);

        }

        public function mouseclicked(e:MouseEvent):void {
            trace("clicked");
            file = new searchForFiles();
            file.addEventListener(Event.COMPLETE, puttheimage);     
        }
        private function erroremanip(e:IOError):void {
            trace("ciao erroreio");
        }
        private function puttheimage(e:Event) :void {
            addChild(file.loader);

        }
    }   
}

2 个答案:

答案 0 :(得分:0)

FileReference用于访问用户本地计算机上的文件。听起来您想要从托管SWF文件的同一服务器加载文件。

您不能从Actionscript'浏览'服务器 - 除非您在服务器上编写代码以启用它 - 但您可以使用URLLoader按名称加载文件。

答案 1 :(得分:0)

  

调用FileReference.browse()   当需要通过用户交互IE:mouseclick触发外部本地沙箱时。
  基本上,click事件需要在某处堆栈中   您可以使用。

进行验证
    file = new FileReference();
    file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

但是,您没有发布任何代码,而且很难准确确定您做错了什么。

[编辑]

package searchfiles 
{
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.net.FileReference;
    import flash.net.FileReferenceList;
    import flash.net.FileFilter;
    import flash.events.*;
    import flash.net.FileFilter; 
    import flash.net.FileReference; 
    import flash.net.URLRequest; 
    import flash.utils.ByteArray; 
    import flash.display.DisplayObject;

    /**
     * ...
     * @author ddd
     */
    public class searchForFiles extends EventDispatcher
    {
        public var newfile:FileReference;
        public var loader:Loader
        public var bitmapimg:BitmapData;            

        public function searchForFiles() {
            newfile = new FileReference();
            newfile.addEventListener(Event.SELECT, onFileSelected); 
            newfile.addEventListener(Event.CANCEL, onCancel); 
            newfile.addEventListener(IOErrorEvent.IO_ERROR, onIOError); 
            newfile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);   
        }


             // new function
        public function browse(event:Event):void{
            var textTypeFilter:FileFilter = new FileFilter("Image files (*.png, *.jpg, *tif)", "*.png; *.jpg; *tif");   
            newfile.browse([textTypeFilter]);
        }




        public function onFileSelected(evt:Event):void 
        { 
                    newfile.addEventListener(ProgressEvent.PROGRESS, onProgress); 
                    newfile.addEventListener(Event.COMPLETE, onComplete); 
                    newfile.load(); 
        } 

        public function onProgress(evt:ProgressEvent):void 
        { 
            trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes."); 

        } 

        public function onComplete(evt:Event):void 
        { 
            trace("File was successfully loaded."); 
            loader = new Loader();              
            loader.loadBytes(newfile.data);         
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);           
        } 

        private function erroremanip(evt:IOErrorEvent):void {
            trace("errore " + evt);
        }
        private var bitmapData:BitmapData

        public function getBitmapData(e:Event):void {
            var content:* = loader.content;
            bitmapData = new BitmapData(content.width,content.height,true,0x00000000);
            trace("loader.width = " +loader.width);
            dispatchEvent( new Event(Event.COMPLETE));
            //trace("get bitmap data called");
        }

        public function onCancel(evt:Event):void 
        { 
            trace("The browse request was canceled by the user."); 
        } 

        public function onIOError(evt:IOErrorEvent):void 
        { 
            trace("There was an IO Error."); 
        } 
        public function onSecurityError(evt:Event):void 
        { 
            trace("There was a security error."); 
        }                       
    }    
}

这里是main()

package 
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.errors.IOError;
    import flash.events.*;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import searchfiles.searchForFiles;

    /**
     * ...
     * @author ddd
     */
    [SWF(width = "550", height = "600")]

    public class Main extends MovieClip 
    {
        public var file:searchForFiles;
        public var mybtn:Loader = new Loader();

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point


                     // moved to init
            file = new searchForFiles();
            file.addEventListener(Event.COMPLETE, puttheimage);



            mybtn.addEventListener(MouseEvent.CLICK, mouseclicked);
            mybtn.addEventListener(IOErrorEvent.IO_ERROR, erroremanip);
            var urlqst:URLRequest = new URLRequest("preview_true.png");
            mybtn.load(urlqst);
            addChild(mybtn);


        }

        public function mouseclicked(e:MouseEvent):void {
            trace("clicked");
                    // events need to be set before any active code is run in the object
                    // that is why we moved listeners or else you risk the listener
                    // not getting triggered
                    file.browse()
        }
        private function erroremanip(e:IOError):void {
            trace("ciao erroreio");
        }
        private function puttheimage(e:Event) :void {
            addChild(file.loader);

        }
    }   
}