0 Replies Latest reply: Jul 31, 2013 11:27 PM by V.K.R (Vijay) RSS

    How to cancel the file download

    V.K.R (Vijay) Community Member

      I am using the filestream method for downloading the file from the server.

       

      I need an option for canceling the download

       

      how it possible?

       

      my code

      private var request:URLRequest = new URLRequest;

                                              private var stream:URLStream = new URLStream();

       


      private var fileStream:FileStream = new FileStream();

                          private function downloadHelpDoc():void

                                              {

                                                        popDownload = new HelpDownloadWindow;

                                                        PopUpManager.addPopUp(popDownload, FlexGlobals.topLevelApplication as DisplayObject,false);

                                                        PopUpManager.centerPopUp(popDownload);

                                                        stream.addEventListener(ProgressEvent.PROGRESS, onDownloadProgress);

                                                        stream.addEventListener(Event.COMPLETE,onComplete);

                                                        stream.addEventListener(IOErrorEvent.IO_ERROR, onDownloadIoError);

                                                        stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onDownloadSecurityError); 

                                                        request.url = serverUrl+fileName;

                                                        stream.load(request);

                                              }

       

       

                                              // to open the PDF file after the download

                                              private function onComplete(event:Event):void

                                              {

                                                        popDownload.btnView.visible = true;

                                                        popDownload.btnView.setFocus();

                                                        popDownload.warningMsg.visible=true;

       

                                                        var fileData:ByteArray = new ByteArray();

                                                        stream.readBytes(fileData, 0, stream.bytesAvailable);

       

                                                        fileStream.open(file, FileMode.WRITE);

                                                        fileStream.writeBytes(fileData, 0, fileData.length);

                                                        fileStream.close();

                                              }

       

                                              // to show the progressbar based on the download of the help pdf file

                                              private function onDownloadProgress(event:ProgressEvent):void

                                              {

                                                        try

                                                        {

       

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

                                                                  popDownload.progBar.setProgress(event.bytesLoaded, event.bytesTotal);

                                                        }

                                                        catch (ex:Error)

                                                        {

                                                                  Alert.show(ex.message.toString());

                                                        }

                                              }

       

                                              // Called on downlaod io error

                                              private function onDownloadIoError(event:IOErrorEvent):void {

       

                                                        Alert.show(event.text.toString());

                                              }

       

                                              // Called on downlaod security error

                                              private function onDownloadSecurityError(event:SecurityErrorEvent):void {

                                                        Alert.show(event.text.toString());

                                              }