使用Flash AS3强制下载

时间:2011-12-11 06:59:16

标签: flash actionscript-3

我想只使用Flash AS3强制下载功能,是否可能?我试过谷歌但失败了。这是我的as3代码......

var file_URLRequest:URLRequest = new URLRequest ("mp3gallery/" + url);
var content_header:URLRequestHeader = new URLRequestHeader("Content-Type: application/force-download");
var attach_header:URLRequestHeader = new URLRequestHeader("Content-Disposition: attachment; filename=abc.mp3");

            file_URLRequest.requestHeaders.push(content_header);
            file_URLRequest.requestHeaders.push(attach_header);
            file_URLRequest.method = URLRequestMethod.POST;

            navigateToURL(file_URLRequest, '_blank');

谢谢。

1 个答案:

答案 0 :(得分:1)

这是directly from the docs

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

编辑:如果以这种方式使用,则无法使用,因为可能需要用户的操作(单击)。因此,您应该将构造函数中包含的代码移动到方法(单击处理程序)。当然,在这种情况下,btn是放置在舞台上的动画片段的实例名称,而FileReference_download是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);
        }