AS3 Download start function after pressing a button!

I need an actionscript 3 function for my site that allows users to download a document after clicking a button.

Could not find this on the net.

Thanks! Jennifer

+4
source share
2 answers

FileReference :: download ()

btn.addEventListener(MouseEvent.CLICK, promptDownload); private function promptDownload(e:MouseEvent):void { req = new URLRequest("http://example.com/remotefile.doc"); file = new FileReference(); file.addEventListener(Event.COMPLETE, completeHandler); file.addEventListener(Event.CANCEL, cancelHandler); file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); file.download(req, "DefaultFileName.doc"); } private function cancelHandler(event:Event):void { trace("user canceled the download"); } private function completeHandler(event:Event):void { trace("download complete"); } private function ioErrorHandler(event:IOErrorEvent):void { trace("ioError occurred"); } 
+5
source

If you make a button and give it an iBtn_Download instance iBtn_Download , the code to make it work is as follows. Just paste the following code into your project schedule. Just change the website address of the template where your document is located.

 iBtn_Download.addEventListener(MouseEvent.CLICK, downloadDocument); function downloadDocument(_event:MouseEvent):void { var urlRequest:URLRequest = new URLRequest("http://www.yourwebsite.com/downloads/document.pdf"); navigateToURL(urlRequest); } 
0
source

Source: https://habr.com/ru/post/1311834/


All Articles