Question

newbie on AS3 here! :)

basically I'm trying to write an application that let the user choose an image file and display it (then I will manipulate pixels, so i don't want the application to store the image in a new file, just managing the ByteArray).

So far I wrote in Flash Develop some working code that show a window to choose the image and then display it. but when I upload to a server the generated files (myapplication.swf, expressinstall.swf, index.html, and the js folder) the window shows no more.

I'm using FileReference.browse() method.

What's wrong?

(edit: as pointed out from The_asMan here we miss some code, here it is - improved with the suggestion of The_asMan)

my package:

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

and here's the 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);

        }
    }   
}
Was it helpful?

Solution

FileReference.browse() When outside local sandbox needs to be triggered via user interaction IE: mouseclick.
Basically, the click event needs to be in the stack somewhere.
You can verify this with.

    file = new FileReference();
    file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

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

However, you posted no code and it is very hard to determine exactly what you did wrong.

[EDIT]

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

and here's the 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);

        }
    }   
}

OTHER TIPS

FileReference is for accessing files on the user's local machine. It sounds like you want to load a file from the same server that is hosting the SWF files.

You can't 'browse' the server from Actionscript - unlesss you write code on your server to enable that - but you can load files by name using URLLoader.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top