强制下载使用Flash AS3下载使用、Flash

由网友(社会狗多ゞ小心被咬)分享简介:我想有力量的下载功能只与Flash AS3,有没有可能?我试图谷歌,但失败了。这里是我的AS3 code ...... i want to have force download functionality only with Flash AS3, is it possible ?? i tried google b...

我想有力量的下载功能只与Flash AS3,有没有可能?我试图谷歌,但失败了。这里是我的AS3 code ......

i want to have force download functionality only with Flash AS3, is it possible ?? i tried google but failed. here it is my as3 code......

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

感谢你。

推荐答案

这是的直接从文档:

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);
        }
阅读全文

相关推荐

最新文章