0
votes

i am new here and i really need help. Working on flash project and i encountered a problem. What im trying to do is let the user select the files with restricted file extensions like *.png, *jpg. After files are selected flash shows a little thumbnails of the pictures at the left cornern plus i push the filereferences to the new array so that when the upload button is clicked, files would get uploaded. Project files are here. The thumbnails arrangement is not as i wanted in the provided files but that not the problem.

The problem is when the files are selected function gofurther draws the thumbnails which is fine but once i click upload_btn bytesAr function is being called again and draws all selected images. you can look how it behaves before and after the button is clicked here.

The part that gives me this problem is this:

function uplFile(e:Event):void
        {
            for (var i:Number=0; i<fileUp.length; i++)
            {
                fileUp[i].upload(loaDur);// this line
            }
        }

If i comment the line inside loop which uploads the files, than everything is fine, but if i leave it it calles the bytesar function, draws the thumbnails from fileUp array all over again even though the parameters that i gave to that function is file:filereferenceList, plus there is no bytesarr function reference in the uplFile function so how is it being called? Can someone help? thanks

public class uplClass extends MovieClip
{
    var filter:Array = new Array(new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)","*.jpg; *.jpeg; *.gif; *.png"));
    var loaDur:URLRequest = new URLRequest(( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : './upload.php');
    var file:FileReferenceList;
    var fileUp:Array = new Array();
    var numb:Number = 0;
    var track:Number = 0;
    var loadeR:Loader;
    var bitmap:Bitmap;

    public function uplClass()
    {
        file = new FileReferenceList();
        setup(file);
        select_btn.addEventListener(MouseEvent.CLICK, browse);
        //cancel_btn.addEventListener(MouseEvent.CLICK, cancel_func);
        upload_btn.addEventListener(MouseEvent.CLICK, uplFile);
        cancel_btn.visible = false;
        upload_btn.visible = false;
    }
    public function browse(e:MouseEvent)
    {
        file.browse(filter);
    }
    private function setup(file:FileReferenceList)
    {
        file.addEventListener(Event.SELECT, select_func);
    }
    private function error_func( e:IOErrorEvent )
    {
        var tf = new TextFormat();
        tf.color = 0xff0000;
        label_txt.defaultTextFormat = tf;
        label_txt.text = 'The file could not be uploaded.';
        cancel_btn.visible = false;
        select_btn.visible = true;
    }
    private function select_func( e:Event )
    {
        for each (var fileRef:FileReference in file.fileList)
        {
            fileUp.push(fileRef);
            fileRef.load();
            fileRef.addEventListener(Event.COMPLETE, bytesAr);
        }
        function bytesAr(e:Event):void
        {
            loadeR = new Loader();
            loadeR.loadBytes(e.target.data);
            loadeR.contentLoaderInfo.addEventListener(Event.COMPLETE, gofurther);
            //track++;
            //count_txt.text=file.toString()+"1 "+fileUp.toString()+" "+numb;
            //trace(file+"1 "+fileUp);
        }
    }

     function gofurther(e:Event):void
    {
        bitmap = e.target.content;
        bitmap.scaleX = 0.2;
        var thumbLoad:Sprite = new Sprite  ;
        thumbLoad.graphics.beginBitmapFill(bitmap.bitmapData,null,false,true);
        thumbLoad.graphics.drawRect(0,0,30,30);
        thumbLoad.graphics.endFill();
        thumbLoad.x = 100 * numb;
        addChild(thumbLoad);
        numb++;
        //count_txt.appendText(numb+" ");
        if (upload_btn.visible == false)
        {
            upload_btn.visible = true;
        }
    }
    function uplFile(e:Event):void
    {
        for (var i:Number=0; i<fileUp.length; i++)
        {
            fileUp[i].upload(loaDur);// if i comment this line everything is fine, but if i leave it like this it calls bytesAr function somehow
        }
    }
}

}

PHP code to move uploaded files:

$uploads_dir = './uploads/';

if( $_FILES['Filedata']['error'] == 0 ){ if( move_uploaded_file( $_FILES['Filedata']['tmp_name'], $uploads_dir.$_FILES['Filedata']['name'] ) ){ echo 'ok'; exit(); } } echo 'error'; exit();

1

1 Answers

0
votes
fileUp.push(fileRef);
fileRef.load();
fileRef.addEventListener(Event.COMPLETE, bytesAr); // <- see this? where are you removing it?

Now check the upload() of FileReference: complete:Event — Dispatched when the file upload operation completes successfully.

You are actually firing the same listener after the upload completes. Hope it helps.