tags:

views:

302

answers:

2

I am a novice to flex and I am trying to develop an application for uploading multiple image file along with progressbar for each upload. The datagrid holds the name of the file and the progressbar for each file when we select and add a file. When there is progress in file upload, it should be reflected in the progressbar as well. I have used filerefencelist to hold the collection of files and when i try to upload files it generates error "Null object"

My code :

private var initDG : ArrayCollection;
        private var _arrUploadFiles:Array = new Array();
        private var currentFile:FileReference;
        private var currentFileIndex:Number = 0;
        private var uploadErrors:Array = new Array();
        private var _refAddFiles:FileReferenceList = new FileReferenceList();   

// function in called when upload button is clicked
         // uploading file to server
         private function serverFileupload(event:Event):void
          {
            if( ! __serverSideScriptURL )
             {
               Alert.show("Server fileupload URL is missing. Unable to upload.");
               return;
             }


        var request:URLRequest = new URLRequest();
        request.url = __serverSideScriptURL;
        request.method = URLRequestMethod.POST;
        currentFile = new FileReference();
        currentFile = _arrUploadFiles[currentFileIndex];

        currentFile.addEventListener(IOErrorEvent.IO_ERROR, uploadIoErrorHandler);
        currentFile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler );

        currentFile.upload(request, "FileUpload", false);    
      }


Can anyone help me out with the solution to this problem?

A: 

Where do you fill _arrUploadFiles[]?

In your code above you new an object and then overwrite the reference. Maybe this is the source of your error. Try this...

    currentFile = new FileReference();
    _arrUploadFiles[currentFileIndex] = currentFile;

    currentFile.addEventListener(IOErrorEvent.IO_ERROR, uploadIoErrorHandler);
    currentFile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler );

    currentFile.upload(request, "FileUpload", false);  

I also don't see anywhere in your code above where you set the event handler to update a progress bar, or where the progress bar is set.

Maybe I'm off track, in which case you will need to post a bit more code.

Simon
A: 

There are some obvious problems in your code. You should probably address these first. Start here:

    currentFile = new FileReference();
    currentFile = _arrUploadFiles[currentFileIndex];

In these two lines, you've created a new FileReference and then immediately discarded it and replaced it with _arrUploadFiles[currentFileIndex]. Furthermore, if the code you've pasted is all of the relevant code (it seems incomplete), then you're not going to have the effect you are hoping for because the value of _arrUploadFiles[currentFileIndex] is going to be undefined (as no value has been set in _arrUploadFiles at that index yet).

Once you've addressed some of the elementary problems, read the Flex documentation on FileReference. There are some examples in those docs that you should study carefully. Note how the FileReference.browse() method is used to open a file dialog which allows the user to choose which file(s) should be uploaded. There's no point in calling upload() until you've populated the FileReference in this manner.

erikprice