|
-
File download / URLRequest
Hi,
I'm trying to create a button that downloads an mp3 file and saves it to a users computer ie. you click the button and the standard windows box appears asking where you want to save the file.
I've tried the code below but the file opens and starts playing as opposed to asking the user where to save it. Am I on the right lines for achieving this?
Thanks for any pointers.
Robin
btn.addEventListener(MouseEvent.CLICK, myButtonFunction);
function myButtonFunction(event: MouseEvent)
{
var request:URLRequest = new URLRequest("test.mp3");
navigateToURL(request, "_blank");
}
-
Senior Member
Use the FileReference class.
- The right of the People to create Flash movies shall not be infringed. -
-
Thanks for that.. I've literally just figured that out and was about to post an answer to my own question.. but you beat me to it!
-
For the person who asked and anyone else here's the code:
package
{
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileReference;
import flash.net.URLRequest;
import fl.controls.Button;
public class FileReference_download extends Sprite
{
//private var btn:Button;
private var downloadURL:URLRequest;
private var fileName:String = "test.mp3";
private var file:FileReference;
public function FileReference_download()
{
btn.addEventListener(MouseEvent.CLICK,downloadIt);
}
private function downloadIt(e:MouseEvent):void
{
downloadURL = new URLRequest();
downloadURL.url = "http://www.yourdomain.com/test.mp3";
file = new FileReference();
configureListeners(file);
file.download(downloadURL, fileName);
}
private function configureListeners(dispatcher:IEventDispatcher):vo id
{
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.SEC URITY_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):voi d
{
trace("securityErrorHandler: " + event);
}
private function selectHandler(event:Event):void
{
var file:FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name + " URL=" + downloadURL.url);
}
}
}
-
Thanks!
I've been looking for something like this for a long time.
-
1 click to download 1 song, so difficult!
I use Actionscrip 3.0
I have a button, draged it to the scene and gave it an instance name of btn.
I used the code(above) in the document class, and I get the errors :
1120 - Access of undefined property btn
1172 - Definition fl.controls:Button cound not be found.
I cant figure It out,
and need help.
Thanks alot!
Jimmy
-
Senior Member
You need to import the class:
import fl.controls.Button;
- The right of the People to create Flash movies shall not be infringed. -
-
Thanks for the quick answer! I made some research, I evidently had to drag a component button to the library before the import the class: import fl.controls.Button; could even work.
Great! now I don't get the error: Definition fl.controls:Button cound not be found.
But I have an image on the stage that i've converted to a button an given the instance name btn. I still get the error:
-Access of undefined property btn.
on line 21 in the document class, wich is: btn.addEventListener(MouseEvent.CLICK,downloadIt);
/Jimmy
-
You will probably need to define the name of your custom button that you are using in the class. If you've just copied and pasted the code from this post .. try uncommenting //private var btn:Button;
-
Thanks, Im getting closer...
Now Im getting error: A conflict exists with definition btn in namespace internal.
-
Senior Member
You have multiple instances in the same class of a variable called btn ...i.e. multiple var btn: declarations. So if you just uncommented the //private var btn:Button, make sure that further on in your code you just say btn = new Button as opposed to var btn:Button = new Button -- you can't declare it twice.
-
Senior Member
You need to go to "Settings" and uncheck "Automatically declare stage instances". Then in your Document class you need to declare btn as public, because it is already on the stage.
public var btn:Button;
- The right of the People to create Flash movies shall not be infringed. -
-
thanks, I don't get that error no more. I got a new error: "Call to a possibly undefined method addFrameScript." and suddenly my tweens get errors:Access of undefined property. (They worked before)
I found this, "Make the class extend MovieClip instead of Sprite. Sprite does not have a timeline. You are probably attempting to add code to a frame on the main timeline. If you are, you need to use MoveClip."
When I do it, I don't get any errors from the document class! but my tweens dont work. Any ideas on that?
-
Senior Member
You need to extend the MovieClip class, since the Sprite has no timeline.
- The right of the People to create Flash movies shall not be infringed. -
-
Thanks for the help, It should work now! extend MovieClip instead of Sprite made it.
Now I'll define my Tweener Movieclips in the document class as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|