4 Replies Latest reply on Aug 11, 2010 10:56 AM by chris.campbell

    How to download images/data without user interaction

    zeroskillz Level 3

      I'm developing a kiosk application that I would like to have download files to the local filesystem without user interaction. This is easy enough for most things by loading it into the app and saving it out, but I'm hoping there is a more direct-to-disk method, so to speak (or if there's a good way to read in and save that doesn't involve things like re-encoding a jpeg--maybe a read and write bytes something?).

       

      Is this possible? Mind you I don't want any dialog boxes popping up.

       

      Thanks in advance!

      -Zeke

        • 1. Re: How to download images/data without user interaction
          thilgen Level 4

          AIR has full access to the file system - check out the FileStream classes for examples of how to write data to disk.

           

          As for pulling down data, you can use the URLLoader class to download any content from the web, without any kind of UI, and then use the URLLoader.bytesLoaded and URLLoader.data properties to read the data that was downloaded.

           

          Hope that helps.

           

          Chris Thilgen

          AIR Engineering

          • 2. Re: How to download images/data without user interaction
            zeroskillz Level 3

            Ah yes, there it is

             

            My major mental roadblock was images, since in my brain I was thinking I had to load them as bitmaps, then re-encode and save as jpeg.

            Got it, get the bytes, then simply writeBytes as I would normally.

             

            In case anyone ends up on this post from a search, here's a simple download class for Air.

            Could use some cleaning up I'm sure...

             

            package ted.air.filesystem
            {
                 
                 import flash.display.*;
                 import flash.net.*;
                 import flash.events.*;
                 import flash.filesystem.*;
                 import flash.utils.ByteArray;
                 
                 public class Download extends Sprite
                 {
                      private var _fileLoader:URLLoader;
                      private var _fileName:String;
                      private var _downloadsDirectory:File;
                      
                      public function Download(fileURL:String, fileName:String = null):void
                      {
                           if(fileName){_fileName = fileName}
                           else{_fileName = fileURL.substr(fileURL.lastIndexOf("/")+1);}
                           var userDir:File = File.userDirectory;
                           _downloadsDirectory = userDir.resolvePath("My Documents/Downloads/");
                           
                           _fileLoader = new URLLoader(new URLRequest(fileURL));
                           _fileLoader.dataFormat = URLLoaderDataFormat.BINARY;
                           configureListeners();
                      }
                      
                      private function completeHandler(event:Event):void
                      {
                           trace("completeHandler: " + event);
                           var fileData:ByteArray = event.target.data;
            
                           var dlFile:File = File.userDirectory;
                           dlFile = _downloadsDirectory.resolvePath(_fileName);
                           
                           var dlFileStream:FileStream = new FileStream();
                           dlFileStream.open(dlFile, FileMode.WRITE);
                           dlFileStream.writeBytes(fileData);
                           dlFileStream.close();
                           trace("fileSaved: "+dlFile.nativePath);
                           configureListeners(true);
                      }
                      
                      private function httpResponseStatusHandler(event:HTTPStatusEvent):void 
                      {
                           trace("httpResponseStatusHandler: " + event);
                           trace("\tstatus: " + event.status);
                           trace("\tresponseURL: " + event.responseURL);
                           trace("\tresponseHeaders:");
                           for each(var responseHeaderObject in event.responseHeaders)
                           {
                                trace("\t\tname: " + responseHeaderObject.name + ", Value: " + responseHeaderObject.value);
                           }
                            }
                      
                      private function httpStatusHandler(event:HTTPStatusEvent):void 
                      {
                           trace("httpStatusHandler: " + event);
                           trace("\tstatus: " + event.status);
                         }
            
                      private function cancelHandler(event:Event):void 
                      {
                           trace("cancelHandler: " + event);
                           configureListeners(true);
                         }
                  
                      private function ioErrorHandler(event:IOErrorEvent):void 
                      {
                           trace("ioErrorHandler: " + event);
                           configureListeners(true);
                      }
                      
                      private function openHandler(event:Event):void 
                      {
                           trace("openHandler: " + event);
                      }
                      
                      private function progressHandler(event:ProgressEvent):void 
                      {
                           trace("progressHandler bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
                      }
                      
                      private function securityErrorHandler(event:SecurityErrorEvent):void 
                      {
                           trace("securityErrorHandler: " + event);
                           configureListeners(true);
                      }
                      
            
                      //-------------------------------------------
                      // Helpers
                      //-------------------------------------------
            
                      private function configureListeners(remove:Boolean = false):void 
                      {
                           if(!remove)
                           {
                                _fileLoader.addEventListener(Event.COMPLETE, completeHandler);
                                _fileLoader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpResponseStatusHandler);
                                _fileLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                                _fileLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                                _fileLoader.addEventListener(Event.OPEN, openHandler);
                                _fileLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                                _fileLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                           }
                           else
                           {
                                _fileLoader.removeEventListener(Event.COMPLETE, completeHandler);
                                _fileLoader.removeEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpResponseStatusHandler);
                                _fileLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                                _fileLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                                _fileLoader.removeEventListener(Event.OPEN, openHandler);
                                _fileLoader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
                                _fileLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                           }
                      }
            
            
            
                 }
            }
            
            • 3. Re: How to download images/data without user interaction
              chris.campbell Adobe Employee

              Moved thread to the Adobe AIR Development forum

              • 4. Re: How to download images/data without user interaction
                thilgen Level 4

                Thanks for sharing your sample code!

                 

                Chris Thilgen

                AIR Engineering