Skip navigation
Currently Being Moderated

How to cancel the file download

Jul 31, 2013 11:27 PM

Tags: #4.5

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());

                                        }

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points