Skip to main content
Participant
April 16, 2007
Question

Handle file download

  • April 16, 2007
  • 8 replies
  • 652 views
I wanna write a little flash, that downloads an mp3 file from the server; then the user can download the file on his coputer.
Can flash do this ?

thanx
    This topic is closed to new replies. Start a new post to keep the conversation going.

    8 replies

    April 17, 2007
    From the livedocs:

    Example

    The following example downloads approximately half of the requested file and then cancels the download. This is obviously not a typical usage. You might more often use this method to allow users to click Cancel in a download status dialog box.

    import flash.net.FileReference;

    var listener:Object = new Object();

    listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
    trace("onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal);
    if(bytesLoaded >= (bytesTotal / 2)) {
    file.cancel();
    }
    }

    var fileRef:FileReference = new FileReference();
    fileRef.addListener(listener);
    var url:String = " http://www.macromedia.com/platform/whitepapers/platform_overview.pdf";
    fileRef.download(url, "FlashPlatform.pdf");

    April 17, 2007
    About cancelling the download, you can do it programatically through actionscript, but I'm not so sure how you'd respond to the window closing. Maybe a javascript onUnload event that makes a call to the flashplayer?

    About saving the file to ram and then writing it out to a file, no, the flashplayer can't do it. Beyond the fileReference class, the Flashplayer has zero access to the local filesystem.
    wdamnAuthor
    Participant
    April 17, 2007
    quote:

    Originally posted by: JayCharles
    About cancelling the download, you can do it programatically through actionscript,

    how can I do it? do you have any examples?

    very kind of you answering :)
    by
    April 16, 2007
    http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?href=00002210.html

    download (FileReference.download method)

    public download(url:String, [defaultFileName:String]) : Boolean

    Displays a dialog box in which the user can download a file from a remote server. Flash Player can download files of up to 100 MB.

    This method first opens an operating-system dialog box that asks the user to enter a filename and select a location on the local computer to save the file. When the user selects a location and confirms the download operation (for example, by clicking Save), the download from the remote server begins. Listeners receive events to indicate the progress, success, or failure of the download. To ascertain the status of the dialog box and the download operation after calling download(), your ActionScript code must listen for events by using event listeners such as onCancel, onOpen, onProgress, and onComplete.

    When the file has successfully downloaded, the properties of the FileReference object are populated with the properties of the local file and the onComplete listener is invoked.

    Only one browse() or download() session can be performed at a time (because only one dialog box can be displayed at a time).

    This method supports downloading of any file type, with either HTTP or HTTPS. You can also send data to the server with the download() call by appending parameters to the URL, for the server script to parse.

    Note: If your server requires user authentication, only SWF files that are running in a browser--that is, using the browser plug-in or ActiveX control--can provide a dialog box to prompt the user for a user name and password for authentication, and only for downloads. For uploads using the plug-in or ActiveX control, and for uploads and downloads using the stand-alone or external player, the file transfer fails.

    When using this method, consider the Flash Player security model:

    * Not allowed if the calling SWF file is in an untrusted local sandbox.
    * The default is to deny access between sandboxes. A website can enable access to a resource by adding a cross-domain policy file.

    For more information, see the following:

    * "Understanding Security" in Learning ActionScript 2.0 in Flash
    * The Flash Player 8 Security white paper at http://www.macromedia.com/go/fp8_security
    * The Flash Player 8 Security-Related API white paper at http://www.macromedia.com/go/fp8_security_apis

    Availability: ActionScript 1.0; Flash Player 8
    Parameters

    url:String - The URL of the file to download to the local computer. You can send data to the server with the download() call by appending parameters to the URL, for the server script to parse. For example: http://www.myserver.com/picture.jpg?userID=jdoe

    On some browsers, URL strings are limited in length. Lengths greater than 256 characters may fail on some browsers or servers.

    defaultFileName:String [optional] - The default filename displayed in the dialog box, for the file to be downloaded. This string cannot contain the following characters: / \ : * ? " < > | %

    If you omit this parameter, the filename of the remote URL is parsed out and used as the default.
    Returns

    Boolean - A value of true if the dialog box in which a user can select a file is displayed. If the dialog box is not displayed, the method returns false. The dialog box could fail to be displayed for any of the following reasons:

    * You did not pass a value for the url parameter.
    * The parameters passed are of the incorrect type or format.
    * The url parameter has a length of 0.
    * A security violation occurred; that is, your SWF file attempted to access a file from a server that is outside your SWF file's security sandbox.
    * Another browse session is already in progress. A browse session can be started by FileReference.browse(), FileReferenceList.browse(), or FileReference.download().
    * The protocol is not HTTP or HTTPS.
    wdamnAuthor
    Participant
    April 17, 2007
    thanks for your answer, I need smt slightly different:
    - stop the download if an event happens, like the user closing the flash player
    - the swf download the file into the ram user, then the user can save the mp3 on his filesystem

    I have to avoid that the user download the file directly from the server.

    thank you
    April 16, 2007
    Not sure where urgent_train got his info, but you can download a file through the flashplayer.

    See the docs for the fileReference object.
    April 16, 2007
    UPD: im sorry for my incompetence.
    April 16, 2007
    no. flash itself cant. use js or projectors (like mdm zinc).