使用FileReference(AS3)选择正确的URL以下载文件

时间:2013-11-11 07:36:49

标签: php actionscript-3 url download filereference

我正试图将我的Flash网站上的文件下载到其他人的硬盘上。我在这个论坛上提出了一两个相似的挑战,但它们比我的复杂得多。文件是.mp3,我不想将它们转换为.zip。所以我使用了FileReference。它似乎工作正常。但最大的麻烦是我必须在代码中选择和写入的URL。我的敌人是不停的困境:

A)   (http://mysite.net/myfolder/myfile.mp3);

or B)   (http://www.mysite.net/myfolder/myfile.mp3);

因为如果网站的访问者没有意识到他的网址包含(或不包含)正确的字符([triple“W”]或http:// [triple“W”]),那么该文件就是“赢了”下载。我不知道如何“融合”或动态链接两个前缀以获得可靠的良好结果...我可以设置两个按钮,但这是非常不寻常的...(顺便说一句,我有一个与Contact Parse完全相同的问题,将php链接到flash以进行邮寄)。请帮我一把!先感谢您!代码是:

var myfileReference:FileReference = new FileReference();
var myRequest:URLRequest = new URLRequest("http://www.mysite.net/myfolder/myfile.mp3");
function downloadFile (event:MouseEvent):void {
    myfileReference.download(myRequest);
}
download_btn.addEventListener(MouseEvent.CLICK, downloadFile);

2 个答案:

答案 0 :(得分:0)

您可以使用LoaderInfo类。 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/LoaderInfo.html

将其实例分配给每个分阶段的DisplayObject。它包含loaderUrl属性,可以帮助您决定使用哪个URL。

答案 1 :(得分:0)

我终于在这里的代码中找到了解决这个问题的方法。您只需要为每个下载文件添加(在.fla中)一个按钮,两个相同的栏(mc)表示进度(“mc_progress”和“mc_loaded”)和一个文本字段以了解操作已完成(txt_prog。文本)。而已。你可以在这里找到整件事:http://dev.tutsplus.com/tutorials/quick-tip-download-files-through-swfs-using-filereference--active-9068

package
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.net.FileReference;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.events.Event;

    public class fileref extends Sprite
    {
        //Donload Buttons
        public var btn_img_download : MovieClip,
                   btn_txt_download : MovieClip,
                   btn_mp3_download : MovieClip,
                   mc_loaded        : MovieClip;

        //Progress Bar
        public var mc_progress : MovieClip,
                   txt_prog    : TextField;

        //Download links Array
        private var Arr_Links  : Array;

        //Default Parent Path for Downloads 
        private var defaultPath : String = "files/";

        //Filen Name
        private var urlName : String;

        //instance of FileReference() Class
        private var fr : FileReference;

        //url of the requested files
        private var req : URLRequest;

        public function fileref() : void
        {
            //Set buttonModes of the download buttons
            btn_img_download.buttonMode = btn_txt_download.buttonMode = btn_mp3_download.buttonMode = true;

            //Set width of the mc_loaded progress bar to 0
            mc_loaded.scaleX = 0;

            //Create list of files to be downloaded
            Arr_Links = ["myimage.jpg","myaudio.mp3","mytext.rtf"];

            req = new URLRequest();
            fr = new FileReference();

            //Download buttons Event Listeners
            btn_img_download.addEventListener( MouseEvent.CLICK,downloadFile );
            btn_mp3_download.addEventListener( MouseEvent.CLICK,downloadFile );
            btn_txt_download.addEventListener( MouseEvent.CLICK,downloadFile );

            fr.addEventListener( ProgressEvent.PROGRESS,progressHandler );
            fr.addEventListener( Event.COMPLETE,completeHandler );

        }

        private function downloadFile( e : MouseEvent ) : void
        {
            //set the download path to the urlName variable according to clicked Download Button
            switch (e.target.name)
            {
                case "btn_img_download":
                urlName = Arr_Links[0];
                break;

                case "btn_mp3_download":
                urlName = Arr_Links[1];
                break;

                case "btn_txt_download":
                urlName = Arr_Links[2];
                break;
            }

            //change text message "progress" to "downloading..." at txt_prog
            txt_prog.text = "downloading...";

            //Assign url to the req
            req.url = defaultPath + urlName;

            //Downlaod requested file
            fr.download( req );         
        }

        private function progressHandler( event : ProgressEvent ) : void
        {           
            mc_loaded.scaleX = (event.bytesLoaded / event.bytesTotal) ;
        }

        private function completeHandler( event : Event ) : void
        {
            //reset progress bar after download is finished
            mc_loaded.scaleX = 0;
            txt_prog.text = "download finished";
        }
    }
}