AS3 Function to start download after clicking a button!

孤街醉人 提交于 2020-01-02 06:10:12

问题


I need an actionscript 3 function for my website, that lets people download a document after they have clicked on a button.

Couldn't find this anywhere on the net.

Thanks! Jennifer


回答1:


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");
}



回答2:


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

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


来源:https://stackoverflow.com/questions/2972207/as3-function-to-start-download-after-clicking-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!