0

试图将文件从我的闪存网站下载到其他人的硬盘驱动器时,我快疯了。我在这个论坛上发现了一两个类似的挑战,但它们比我的要复杂得多。这些文件是 .mp3,我不想将它们转换为 .zip。所以我使用了 FileReference。它似乎工作正常。但最大的麻烦是我必须在代码中选择和编写的 URL。我的敌人是不断陷入困境:

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

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

因为如果站点的访问者没有意识到他的 URL 包含(或不)正确的字符([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);
4

2 回答 2

0

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

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

于 2013-11-11T08:53:44.483 回答
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";
        }
    }
}
于 2013-11-18T08:35:35.223 回答