Skip to main content
Inspiring
March 3, 2011
Question

Error 2037 File Reference

  • March 3, 2011
  • 1 reply
  • 2477 views

Hi to everybody!

I'm trying to build an application where you can save a jpeg from the stage and after u saved on the desktop it's uploading to my server.

In the Main class the important method are:

// the button for save the image

private function btnClickHandler(e:Event):void {  
     downloadClass = new DownloadClass();
     downloadClass.download(sp);
     downloadClass.addEventListener(DownloadClass.FILE_DOWNLOADED, fileDownloadedListener)

}

// after downloading the file i start the upload
private function fileDownloadedListener(e:Event):void {
     uploadClass = new UploadClass();
     uploadClass.uploadFile(downloadClass.getFile())   
}

The download operation i'ts succesfull, i dispatch the FILE_DOWNLOADED event and start the upload operation passing to uploadClass.uploadFile the file reference:

// DownloadClass //

// complete the download operation

private function completeHandler(event:Event):void {
     file = FileReference(event.target);
     dispatchEvent(new Event(DownloadClass.FILE_DOWNLOADED))
}

// public getFile() method in DownloadClass

public function getFile():FileReference{
     return file

}

The problem is that when i start the upload operation i get the error 2037.

This the UploadClass():

package com.cb.utils.image {
   
    /* PHP CODE
    <?php
    if ($_FILES['Filedata']['name']) {
    move_uploaded_file($_FILES['Filedata']['tmp_name'], 'upload_directory/' . basename($_FILES['Filedata']['name']));
    echo "result=ok"
    }else {
    echo "result=ko";
    }
    ?>   
    */
   
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.FileFilter;
    import flash.net.FileReference;
    import flash.net.URLRequest;
    import flash.net.URLVariables;

   
    public class UploadClass extends Sprite {
        private var uploadURL:URLRequest;
        private var file:FileReference;
       
        public function UploadClass() {
            uploadURL = new URLRequest();
            uploadURL.url = "http://localhost/php_code/FileReferenceExample.php";
            file = new FileReference();
            configureListeners();
        }

        private function configureListeners():void {
            file.addEventListener(Event.CANCEL, cancelHandler);
            file.addEventListener(Event.COMPLETE, completeHandler);
            file.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            file.addEventListener(Event.OPEN, openHandler);
            file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
        }

        private function cancelHandler(event:Event):void {
            trace("cancelHandler: " + event);
        }
       
        private function completeHandler(event:Event):void {
            trace("completeHandler: " + event);
        }
       
        private function uploadCompleteDataHandler(event:DataEvent):void {
            //The uploadCompleteData event is not dispatched if data is not returned from the server.
            //trace("uploadCompleteData: " + event.data);
           
            //will receive the data as a string, can easily convert the string into a URLVariables object
            //by passing the string to the URLVariables constructor,
            //or by using the URLVariables class’s decode() method
           
            var strData:String =(event.data);
            var vars:URLVariables = new URLVariables(strData);
            // result e la variabile
            trace('result: '+vars.result)
        }
       
        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }
       
        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
       
        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }
       
        private function progressHandler(event:ProgressEvent):void {
            var file:FileReference = FileReference(event.target);
            trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }
       
        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }
       
        /*private function selectHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
            file.upload(uploadURL);
        }*/
       
        public function uploadFile(imageFile:FileReference):void{
           
             file = FileReference(imageFile);
            try {
                file.upload(uploadURL);
            }
            catch (e:Error) {
                trace("Unable to upload file.    error:"+e.errorID);
            }
        }
    }
}

The difference beatween a normal file reference is that i start the upload calling a method and not after the select event like this:

private function selectHandler(event:Event):void {
     var file:FileReference = FileReference(event.target);
     trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
     file.upload(uploadURL);
}

Hope somebody can reply to me!

thanks

This topic has been closed for replies.

1 reply

Inspiring
March 3, 2011

... so what should i change im my classes???


Inspiring
March 3, 2011

the File and FileReference classes must be initialized to reference a file path, before their properties can be accessed

don`t write

file = new FileReference();

in your coonstructor

but

file = FileReference.desktopDirectory;

or one of the other static properties:

  • File.applicationStorageDirectory—a storage directory unique to each installed AIR application
  • File.applicationDirectory—the read-only directory where the application is installed (along with any installed assets)
  • File.desktopDirectory—the user's desktop directory
  • File.documentsDirectory—the user's documents directory
  • File.userDirectory—the user directory

it doesn`t matter which