A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: simple link to DOWNLOAD a file???

  1. #1
    Senior Member
    Join Date
    Aug 2001
    Posts
    116

    simple link to DOWNLOAD a file???

    I'm hoping this will be easy for someone to answer. I have a .wmv (or .avi, .mp4... ect... ) file that I want someone to be able to download to their computer by clicking a button. NOT open the file in a new window or tab... but actaully bring up a "save or open" window. Is there a simple way to do this? Here's the page I'm working with... very basic, but it helps you see what I'm trying to do... http://kalcotter.com/randell/trainingvids/

    I'm working with CS3 using AS3.

    Thanks for any input!!!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Use the download method of FileReference.

  3. #3
    Senior Member
    Join Date
    Aug 2001
    Posts
    116
    I'm not sure what that means. Does anyone have any example code that they used for this?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    var fr:FileReference = new FileReference();
    fr.download(new URLRequest("somefile.avi"));

  5. #5
    Senior Member
    Join Date
    Aug 2001
    Posts
    116
    Thanks a lot. I can play with this when I get home... but if it's easy for you to answer this, it would help a lot.. lets say my button instance is called avi .... where do I put the code you just sent?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    avi.addEventListener(MouseEvent.CLICK, startDownload);
    
    function startDownload(e:MouseEvent):void{
      var fr:FileReference = new FileReference();
      fr.download(new URLRequest("somefile.avi"));
    }

  7. #7
    Senior Member
    Join Date
    Aug 2001
    Posts
    116
    You rock man. thanks a ton! So, if I had 5 or 6 of these I would have to change the event name or something right? Would it be like this?

    Code:
    avi.addEventListener(MouseEvent.CLICK, startDownload1);
    
    function startDownload1(e:MouseEvent):void{
      var fr:FileReference = new FileReference();
      fr.download(new URLRequest("somefile.avi"));
    }
    
    wmv.addEventListener(MouseEvent.CLICK, startDownload2);
    
    function startDownload2(e:MouseEvent):void{
      var fr:FileReference = new FileReference();
      fr.download(new URLRequest("somefile.avi"));
    }
    
    mp4.addEventListener(MouseEvent.CLICK, startDownload3);
    
    function startDownload3(e:MouseEvent):void{
      var fr:FileReference = new FileReference();
      fr.download(new URLRequest("somefile.avi"));
    }

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could do that (assuming you change the file to download, or there's no reason to use different functions). But what I'd do is set up a mapping between the buttons and the files and use a single handler function anyway.

    You can set up that relationship with a Dictionary, or if your buttons are MovieClips, you can add the property dynamically.

    MovieClip method:
    Code:
    var buttons:Array = [avi, wmv, mp4];
    avi.link = "somefile.avi";
    wmv.link = "somefile.wmv";
    mp4.link = "somefile.mp4";
    
    for (var i:int = 0; i < buttons.length; i++){
      buttons[i].addEventListener(MouseEvent.CLICK, startDownload);
    }
    
    function startDownload(e:MouseEvent):void{
      var button:MovieClip = MovieClip(e.currentTarget);
      var fr:FileReference = new FileReference();
      fr.download(new URLRequest(button.link));
    }
    Dictionary method:
    Code:
    var buttons:Array = [avi, wmv, mp4];
    var links:Dictionary = new Dictionary();
    links[avi] = "somefile.avi";
    links[wmv] = "somefile.wmv";
    links[mp4] = "somefile.mp4";
    
    for (var i:int = 0; i < buttons.length; i++){
      buttons[i].addEventListener(MouseEvent.CLICK, startDownload);
    }
    
    function startDownload(e:MouseEvent):void{
      var fr:FileReference = new FileReference();
      fr.download(new URLRequest(links[e.currentTarget]));
    }

  9. #9
    Senior Member
    Join Date
    Aug 2001
    Posts
    116
    cool man! your way might be over my head, but I'll play with it after work. You've helped me greatly!!! thanks again!

  10. #10
    Senior Member
    Join Date
    Aug 2001
    Posts
    116
    So, I'm home playing with this now. I used this code:

    Code:
    flv.addEventListener(MouseEvent.CLICK, startDownload1);
    function startDownload1(e:MouseEvent):void{
      var fr:FileReference = new FileReference();
      fr.download(new URLRequest("crispease.flv"));
    }
    When I test movie and click the flv button, I'm getting this errror:

    Error: Error #2039: Invalid remote URL protocol. The remote URL protocol must be HTTP or HTTPS.
    at flash.net::FileReference/download()
    at trainingvideos_fla::MainTimeline/startDownload1()




    you can test it here: http://kalcotter.com/randell/trainingvids/

    It opens the 'save as' window, but once you click save, nothing really happens.

    Any ideas?

    Thanks!

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In testing locally, the relative url generated by that code will be a file:// url and won't work. But I'm not sure what's going on with it online. I see the same behavior you described. It doesn't even appear to be making a request to the server.
    You might try using an absolute url in the request.

  12. #12
    Senior Member
    Join Date
    Aug 2001
    Posts
    116
    I tried that... doesn't seem to help. I really have no idea, it's all kind of above my head. this is what I have now:

    flv.addEventListener(MouseEvent.CLICK, startDownload1);
    function startDownload1(e:MouseEvent):void{
    var fr:FileReference = new FileReference();
    fr.download(new URLRequest("http://www.kalcotter.com/randell/trainingvids/crispease.flv"));
    }

    but if you go straight to the link: http://kalcotter.com/randell/trainingvids/crispease.flv it does try to download it... proving that there is in fact a file on the server named that... I'm confused.

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, now you have the page on kalcotter.com but you're requesting the file from www.kalcotter.com. That might cause crossdomain issues.

    Look at the docs for FileReference.download
    http://help.adobe.com/en_US/AS3LCR/F...download%28%29

    Put in listeners for all the error events that might occur.

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