5 Replies Latest reply on Jul 14, 2010 4:06 AM by BhaskerChari

    download file without prompt window in flex?

    Prashant@Mdocs

      how i can download a file from remote server without prompt window(where to save file) in flex.i have already downloaded file from remote server,but during downloading i am getting a prompt window(where to save file).?below is the code which i am using.....................,i want downloaded file is saved to default location on system without prompt window?

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:WindowedApplication

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

       

       

      <mx:Script>

      <![CDATA[

       

       

       

       

      import mx.controls.Text;

       

       

      import mx.controls.Alert;

       

      [

      Bindable]

       

      private var fileRef:FileReference = null;

       

       

      private function downloadFile():void{

       

       

      var urlRequest:URLRequest = new URLRequest("http://appldnld.apple.com/iTunes9/061-7656.20100616.ZTns2/iTunesSetup.exe");

      fileRef =

      new FileReference();

      fileRef.download(urlRequest,

      "iTunesSetup.exe");

      fileRef.addEventListener(ProgressEvent.PROGRESS, setDownloadProgress);

      fileRef.addEventListener(Event.COMPLETE, completeHandler);

       

       

       

      }

       

       

      private function setDownloadProgress(event:ProgressEvent):void{

       

      pbProgress.setProgress(event.bytesLoaded, event.bytesTotal);

      pbProgress.label =

      "Downloading [" + Math.round(event.bytesLoaded / event.bytesTotal * 100).toString() + "%]";

       

       

      }

       

       

      private function completeHandler(event:Event):void{

       

      fileRef =

      null;

      Alert.show(

      "Download Completed");

       

      }

       

       

       

      ]]>

      </mx:Script>

       

      <mx:VBox

       

       

      top="10" left="10">

       

       

      <mx:Text buttonMode="true" click="downloadFile()" text="Download File" textDecoration="underline" fontSize="12"/>

       

       

      <mx:ProgressBar visible="{fileRef != null}" mode="manual" label="" id="pbProgress" labelPlacement="center"/>

      </mx:VBox>

       

       

       

       

      </mx:WindowedApplication>

        • 1. Re: download file without prompt window in flex?
          Matt Le Fevre Level 4

          Topic has come up many times, and nobody seems to have managed a work around, so i'm pretty confident saying it's impossible still.

           

          think about it, if you could do that, imagine the exploitation you could have if you could download stuff without the user knowing about it.

          • 2. Re: download file without prompt window in flex?
            Prashant@Mdocs Level 1

            thanks for ur reply,    actually i have AIR application, And AIR allows File System Access, so i think their must be way to do this by AIR.

            • 3. Re: download file without prompt window in flex?
              BhaskerChari Level 4

              Hi Prashanth,

               

              You can make use of the UrlLoader class instead of FileReference class to download the assets without prompt..As suggested it is almost impossible to eliminate the Prompt dialog box with FileReference class due to Security reasons...

               

              Check the below class for reference...

               

              package

               

               

               

               

               

              cfsc.includes

              {

               

              import flash.display.LoaderInfo;

               

               

              public class DownloadAssets

              {

               

              import flash.events.HTTPStatusEvent;

               

              import flash.events.IOErrorEvent;

               

              import flash.events.SecurityErrorEvent;

               

              import flash.net.URLLoaderDataFormat;

               

              import flash.system.Capabilities;

               

              import flash.events.Event;

               

              import flash.utils.ByteArray;

               

              import flash.net.URLLoader;

               

              import flash.net.URLRequest;

               

              import flash.display.Loader;

               

              import mx.rpc.xml.SimpleXMLDecoder;

               

              import cfsc.model.ModelLocator;

               

              import flash.filesystem.File;

               

              import flash.filesystem.FileStream;

               

              import flash.filesystem.*;

               

              import mx.controls.Alert;

               

              [

              Bindable]

               

              public var model : ModelLocator = ModelLocator.getInstance();

               

               

              private var request:URLRequest = new URLRequest();

               

              private var loader:URLLoader = new URLLoader;

               

               

              private var imgData:ByteArray;

               

              private var directory:File;

               

              private var currentImageName:String = "";

               

              private var currIndex:Number = 0;

               

               

              public function DownloadAssets()

              {

              currIndex = 0;

               

              var myFile:File = File.applicationStorageDirectory.resolvePath("Data");

               

               

              if(myFile.exists)

              {

              myFile.deleteDirectory(

              true);

              }

               

              //deleteDirectoryContents();

              loader.dataFormat = URLLoaderDataFormat.BINARY;

              loader.addEventListener(Event.COMPLETE, onImgLoadComplete);

              loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

              loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);

              loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

              downloadAndSaveAssets();

              }

               

               

              public function downloadAndSaveAssets():void

              {

               

               

              var imageName:String;

               

              var imageUrl:String;

              imageName = model.acAssetsInfo.getItemAt(currIndex).MediaFilePath;

              currentImageName = imageName;

               

               

              if(model.acAssetsInfo.getItemAt(currIndex).MediaType == 1 || model.acAssetsInfo.getItemAt(currIndex).TypeOfMedia == "Default")

              {

              imageUrl = model.serverPath +

              "Data/DefaultMedia/" + imageName;

              }

               

              else if(model.acAssetsInfo.getItemAt(currIndex).MediaType == 2 || model.acAssetsInfo.getItemAt(currIndex).TypeOfMedia == "Location")

              {

              imageUrl = model.serverPath +

              "Data/LocationMedia/" + imageName;

              }

               

              else if(model.acAssetsInfo.getItemAt(currIndex).MediaType == 3 || model.acAssetsInfo.getItemAt(currIndex).TypeOfMedia == "Ad")

              {

              imageUrl = model.serverPath +

              "Data/LocationMedia/" + imageName;

              }

               

              request.url = imageUrl;

              request.useCache =

              false;

              request.manageCookies =

              false;

              request.cacheResponse =

              false;

               

               

              try

              {

              loader.load(request);

              }

               

              catch (err:Error)

              {

               

              trace("Unable to load URL: " + err);

              }

               

              trace("\rStatus: loading remote image ");

              }

               

              private function onImgLoadComplete(event:Event):void

              {

              imgData = event.target.data;

               

               

              trace("\rStatus: image loaded ");

              writeBytes();

              currIndex++;

              toDownload();

              }

               

              private function securityErrorHandler(event:SecurityErrorEvent):void

              {

               

              trace("securityErrorHandler: " + event);

              currIndex++;

              toDownload();

              }

               

               

              private function httpStatusHandler(event:HTTPStatusEvent):void

              {

               

              trace("httpStatusHandler: " + event);

              }

               

               

              private function ioErrorHandler(event:IOErrorEvent):void

              {

               

              trace("ioErrorHandler: " + event);

              currIndex++;

              toDownload();

              }

               

              private function writeBytes():void

              {

               

               

              trace("\rStatus: writing file locally ");

               

              var directory:File = File.applicationStorageDirectory.resolvePath("Data");

               

               

              if(!directory.exists)

              {

              directory.createDirectory();

              directory = directory.resolvePath(currentImageName.toString());

              }

               

              else

              {

              directory = directory.resolvePath(currentImageName.toString());

              }

               

               

              var myFileStream:FileStream = new FileStream();

              myFileStream.open(directory, FileMode.WRITE);

              myFileStream.writeBytes(

              this.imgData, 0, this.imgData.length);

              myFileStream.close();

              }

               

              /*private function toDownload():void

              {

              if(model.acAssetsInfo.length > currIndex)

              {

              downloadAndSaveAssets() ;

              }

              else

              {

              //getDirectoryContents(); // This conditions will be satisfied when all the media files are downloaded and starts the transition play timer(setAssetsTimer)

              }

              if(currIndex == 5)

              {

              getDirectoryContents() ; // Calling the transition play timer(setAssetsTimer) when the currIndex is 5 without waiting for all media files to be download

              }

              }*/

               

              private function toDownload():void

              {

               

              if(model.acAssetsInfo.length > currIndex)

              {

              downloadAndSaveAssets() ;

              }

               

              else// This conditions will be satisfied when all the media files are downloaded and starts the transition play timer(setAssetsTimer)

              {

               

              if(model.acAssetsInfo.length < 5)

              {

              getDirectoryContents();

              }

              }

               

              if(currIndex == 5)

              {

              getDirectoryContents() ;

              // Calling the transition play timer(setAssetsTimer) when the currIndex is 5 without waiting for all media files to be download

              }

              }

               

              private function getDirectoryContents():void

              {

               

              //if(currIndex >= model.acAssetsInfo.length)

              {

              model.isSynchnozingWithServer =

              false;

              }

              directory = File.applicationStorageDirectory.resolvePath(

              "Data");

               

              if(directory.exists)

              {

              model.arrAssets = directory.getDirectoryListing();

              model.mainView.setAssetsTimer();

              }

              }

               

              private function deleteDirectoryContents():void

              {

               

              var directory:File = File.applicationStorageDirectory.resolvePath("Data");

               

              if(directory.exists)

              {

              model.arrAssets = directory.getDirectoryListing();

               

              for(var intLoop:int=0;intLoop<model.arrAssets.length;intLoop++)

              {

              directory = directory.resolvePath(model.arrAssets[intLoop].nativePath);

               

              if(directory.exists)

              {

              directory.addEventListener(Event.COMPLETE, completeHandler);

              directory.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandlerFun);

               

              //directory.deleteFileAsync();

               

              if(directory.nativePath != model.strFilePathInUse)

              {

               

              //Alert.show(model.strFilePathInUse);

              directory.deleteFile();

              }

              }

              }

              }

              }

               

              private function completeHandler(event:Event):void

              {

               

              trace("Successfully deleted.");

              }

               

              private function ioErrorHandlerFun(event:Event):void

              {

               

              if(event.type == "ioError")

              {

               

              trace(event.type.toString());

              }

              }

              }

              }

               

              Thanks,

              Bhasker Chari

              • 4. Re: download file without prompt window in flex?
                Prashant@Mdocs Level 1

                thanks for ur reply,...............is there any demo simple code for downloadin

                g files from server  in air application using urlstream and filestream class?plz give me...thanks

                • 5. Re: download file without prompt window in flex?
                  BhaskerChari Level 4

                  Hi Prashanth,

                   

                  Check out the below links these may be helpful:

                   

                  http://stackoverflow.com/questions/342994/download-a-file-with-adobe-air

                   

                  http://stackoverflow.com/questions/3061364/filestream-problem

                   

                  http://www.actionscript.org/forums/showthread.php3?t=202599

                   

                   


                  If this post answers your question or helps, please kindly mark it as such.


                  Thanks,

                   

                  Bhasker Chari