且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

强制下载使用Flash AS3

更新时间:2022-11-28 11:38:35

这是的直接从文档

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.FileReference;
    import flash.net.URLRequest;
    import flash.net.FileFilter;

public class FileReference_download extends Sprite {
    private var downloadURL:URLRequest;
    private var fileName:String = "SomeFile.pdf";
    private var file:FileReference;

    public function FileReference_download() {
        downloadURL = new URLRequest();
        downloadURL.url = "http://www.[yourDomain].com/SomeFile.pdf";
        file = new FileReference();
        configureListeners(file);
        file.download(downloadURL, fileName);
    }

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.CANCEL, cancelHandler);
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        dispatcher.addEventListener(Event.OPEN, openHandler);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        dispatcher.addEventListener(Event.SELECT, selectHandler);
    }

    private function cancelHandler(event:Event):void {
        trace("cancelHandler: " + event);
    }

    private function completeHandler(event:Event):void {
        trace("completeHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }

    private function openHandler(event:Event):void {
        trace("openHandler: " + event);
    }

    private function progressHandler(event:ProgressEvent):void {
        var file:FileReference = FileReference(event.target);
        trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function selectHandler(event:Event):void {
        var file:FileReference = FileReference(event.target);
        trace("selectHandler: name=" + file.name + " URL=" + downloadURL.url);
    }
}
}

编辑:如果用这样的方式,因为可能需要在用户(点击)的动作,这可能无法工作。所以,你应该将包含在构造函数的方法的code(Click处理程序)。当然,在这种情况下, BTN 是一个影片剪辑放置在舞台上的实例名,而 FileReference_download 是的DocumentClass。

this could not work if used this way, because an action of the user (a click) may be required. So you should move the code contained in the constructor to a method (click handler). Of course, in this case btn is the instance name of a movieclip placed on stage, while FileReference_download is the DocumentClass.

 public function FileReference_download() {
            btn.addEventListener(MouseEvent.MOUSE_DOWN, downloadMyFile);
        }

        private function downloadMyFile(e:MouseEvent){
            downloadURL = new URLRequest();
            downloadURL.url = "http://www.[yourDomain].com/SomeFile.pdf";
            file = new FileReference();
            configureListeners(file);
            file.download(downloadURL, fileName);
        }