AS3单击按钮后开始下载的功能!

时间:2010-06-04 07:24:49

标签: actionscript-3 file button download

我的网站需要一个actionscript 3功能,让人们在点击按钮后下载文档。

无法在网上任何地方找到它。

谢谢! 珍

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

如果您创建一个按钮,并为其指定实例名称iBtn_Download,则使其工作的代码如下所示。只需将以下代码粘贴到项目的时间轴中即可。只需将模板网站地址更改为文档所在的位置即可。

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);
}
相关问题