I’m going crazy in vane trying to download files from my flash website to someone else’s hard drive. I red one or two similar challenges at this forum but they are much more complicate than mine. The files are .mp3 and I don’t want to convert them to .zip. So I used FileReference. It seemed to work OK. But the big trouble is the URL I have to choose and write in the code. My enemy is the incessant dilemma to set:
A) (http://mysite.net/myfolder/myfile.mp3);
or B) (http://www.mysite.net/myfolder/myfile.mp3);
Because if the site’s visitor doesn’t realize that his URL contains (or not) the right characters ([triple "W"] or http://[triple "W"]), the file, simply, won’t download. I don’t know how to “fusion” or link both prefixes dynamically to get a infallible good result… I could set two buttons, but that is extremely unusual… (By the way, I had exactly the same problem with the Contact Parse that links php to flash for mailing). Please give me a hand! Thank you in advance! The code is:
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);
I finally found the solution to this problem in the code of the class that I let down here. You just need to add (in the .fla) a button for each downloading file, two identical bars (mc) to indicate the progress ("mc_progress" and "mc_loaded") and a text field to know the action is done (txt_prog.text). That's it. You may find the whole thing here: 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";
}
}
}