5 Replies Latest reply on Oct 16, 2008 2:01 PM by abeall

    Writebytes

    walker6o9
      I'm trying to download a file to disk, but it doesn't seem to be working. I'm not sure what to put for for bytes and length, and if I need to use a URLLoader to load the image first, or if I can write it as long as I have it's path.

      filename = "1.jpg";

      var myFile:File = File.documentsDirectory.resolvePath(filename);
      var myFileStream:FileStream = new FileStream();


      myFileStream.open( myFile, FileMode.WRITE );
      myFileStream.writeBytes(??, 0, ??);



        • 1. Writebytes
          abeall Level 3
          Yes, you need to download with a URLLoader first, and you need to set the URLLoader.dataFormat to "binary". Then, after the URLLoader completes, you can use FileStream.writeBytes with the URLLoader.data property -- don't worry about the offset or length (they are optional).

          const URL_FILE_PATH:String = " http://abeall.com/files/images/RemoveTheLabels.jpg";
          const FILENAME:String = "RemoveTheLabels.jpg";

          var urlLoader:URLLoader = new URLLoader();
          var urlRequest:URLRequest = new URLRequest(URL_FILE_PATH);
          urlLoader.dataFormat = 'binary';
          urlLoader.load(urlRequest);

          urlLoader.addEventListener('complete',urlLoaderComplete);
          urlLoader.addEventListener('progress',urlLoaderProgress);
          urlLoader.addEventListener('ioError',urlLoaderIoError);

          function urlLoaderComplete(event:Event){
          var file:File = File.desktopDirectory.resolvePath(FILENAME);
          var fileStream:FileStream = new FileStream();
          fileStream.open(file,'write');
          fileStream.writeBytes(urlLoader.data);
          }
          function urlLoaderIoError(event:ErrorEvent){
          trace(event.text)
          }
          function urlLoaderProgress(event:ProgressEvent){
          trace(event.bytesLoaded + " / " + event.bytesTotal);
          }
          • 2. Re: Writebytes
            walker6o9 Level 1
            I tried your code, but I got an error, variable FILENAME is not defined. I had to modify your code to go with a dynamically created textlink, so perhaps that is causing problems.
            My code:

            public function linkEvent(event:TextEvent):void {


            const URL_FILE_PATH:String = " http://abeall.com/files/images/RemoveTheLabels.jpg";
            const FILENAME:String = "RemoveTheLabels.jpg";

            var urlLoader:URLLoader = new URLLoader();
            var urlRequest:URLRequest = new URLRequest(URL_FILE_PATH);
            urlLoader.dataFormat = 'binary';
            urlLoader.load(urlRequest);

            urlLoader.addEventListener('complete',urlLoaderComplete);
            urlLoader.addEventListener('progress',urlLoaderProgress);
            urlLoader.addEventListener('ioError',urlLoaderIoError);
            }
            public function urlLoaderComplete(event:Event){
            var file:File = File.desktopDirectory.resolvePath(FILENAME);
            var fileStream:FileStream = new FileStream();
            fileStream.open(file,'write');
            fileStream.writeBytes(urlLoader.data);
            }
            public function urlLoaderIoError(event:ErrorEvent){
            trace(event.text)
            }
            public function urlLoaderProgress(event:ProgressEvent){
            trace(event.bytesLoaded + " / " + event.bytesTotal);
            }


            • 3. Re: Writebytes
              abeall Level 3
              Well, you should replace FILENAME and URL_FILE_PATH with whatever you need it to be, I just put those there as an example of working code.
              • 4. Re: Writebytes
                walker6o9 Level 1
                I think the problem is that FILENAME is not being passed to the urlLoaderComplete event. Is there a way to fix that?
                • 5. Re: Writebytes
                  abeall Level 3
                  Oh I see, you have the linkEvent function, I missed that. Yes, you are right, FILENAME is defined inside likeEvent so the script inside urlLoaderComplete does not know about the variable. It's out of scope, you have two possible solutions that I can think of:

                  1. You could actually nest the urlLoader handler functions(urlLoaderComplete, urlLoaderIoError, urlLoaderProgress) inside the linkEvent function. I don't think you could use the "public" keyword, but you don't need it here anyway.

                  2. You could (and probably should) define the constants FILENAME and URL_FILE_PATH outside the function. Normally you put constants at the top of your class/file, they are sort of like script parameters that you might want to change. So when you come back to edit the class/file you have the FILENAME and URL and such right at the top, you don't have to go hunting for the function that makes the call to the URL, etc. You don't have to use constants at all, though. You could just as well do:

                  var urlRequest:URLRequest = new URLRequest(" http://example.com/image.jpg");
                  ...
                  var file:File = File.desktopDirectory.resolvePath("image.jpg");