A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: File download / URLRequest

  1. #1
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57

    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");
    }

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Use the FileReference class.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57
    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!

  4. #4
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57
    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);
    }
    }
    }

  5. #5
    Junior Member
    Join Date
    Jan 2008
    Posts
    1

    Thanks!

    I've been looking for something like this for a long time.

  6. #6
    Junior Member
    Join Date
    May 2008
    Posts
    5

    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

  7. #7
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to import the class:
    import fl.controls.Button;
    - The right of the People to create Flash movies shall not be infringed. -

  8. #8
    Junior Member
    Join Date
    May 2008
    Posts
    5
    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

  9. #9
    Member
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    57
    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;

  10. #10
    Junior Member
    Join Date
    May 2008
    Posts
    5
    Thanks, Im getting closer...
    Now Im getting error: A conflict exists with definition btn in namespace internal.

  11. #11
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    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.

  12. #12
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    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. -

  13. #13
    Junior Member
    Join Date
    May 2008
    Posts
    5
    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?

  14. #14
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    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. -

  15. #15
    Junior Member
    Join Date
    May 2008
    Posts
    5
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center