News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

ActionScript programming flash:- how to write script for PING and FILE DOWNLOAD

Started by anuraag, Jul 21, 2009, 04:06 PM

Previous topic - Next topic

Kalyan

Working with file upload and download

The FileReference class lets you add the ability to upload and download files between a client and a server. Users are prompted to select a file to upload or a location for download from a dialog box (such as the Open dialog box on the Windows operating system).

Each FileReference object that you create with ActionScript refers to a single file on the user's hard disk. The object has properties that contain information about the file's size, type, name, creation date, and modification date.

NOTE

The creator property is supported on Mac OS only. All other platforms return null.

You can create an instance of the FileReference class in two ways. You can use the new operator, as the following code shows:

import flash.net.FileReference;
var myFileReference:FileReference = new FileReference();

Or you can call the FileReferenceList.browse() method,

which opens a dialog box on the user's system to prompt the user to select one or more files to upload and then creates an array of FileReference objects if the user selects one or more files successfully.

Each FileReference object represents a file selected by the user from the dialog box. A FileReference object does not contain any data in the FileReference properties (such as name, size, or modificationDate) until one of the following happens:

The FileReference.browse() method or FileReferenceList.browse() method has been called, and the user has selected a file from the file picker.
The FileReference.download() method has been called, and the user has selected a file from the file picker.

NOTE

When performing a download, only the FileReference.name property is populated before the download is complete. After the file has been downloaded, all properties are available.

While calls to the FileReference.browse(), FileReferenceList.browse(), or FileReference.download() method are executing, most players will continue SWF file playback.

appu.s8501

Downloading files from a server

You can let users download files from a server using the FileReference.download() method, which takes two parameters: request and defaultFileName. The first parameter is the URLRequest object that contains the URL of the file to download.

The second parameter is optional--it lets you specify a default filename that appears in the download file dialog box. If you omit the second parameter, defaultFileName, the filename from the specified URL is used.

The following code downloads a file named index.xml from the same directory as the SWF document:

var request:URLRequest = new URLRequest("index.xml");
var fileRef:FileReference = new FileReference();
fileRef.download(request);


To set the default name to currentnews.xml instead of index.xml, specify the defaultFileName parameter, as the following snippet shows:

var request:URLRequest = new URLRequest("index.xml");
var fileToDownload:FileReference = new FileReference();
fileToDownload.download(request, "currentnews.xml");


Renaming a file can be very useful if the server filename was not intuitive or was server-generated. It's also good to explicitly specify the defaultFileName parameter when you download a file using a server-side script, instead of downloading the file directly.

For example, you need to specify the defaultFileName parameter if you have a server-side script that downloads specific files based on URL variables passed to it. Otherwise, the default name of the downloaded file is the name of your server-side script.

Data can be sent to the server using the download() method by appending parameters to the URL for the server script to parse. The following ActionScript 3.0 snippet downloads a document based on which parameters are passed to a ColdFusion script:

package
{
    import flash.display.Sprite;
    import flash.net.FileReference;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;

    public class DownloadFileExample extends Sprite
    {
        private var fileToDownload:FileReference;
        public function DownloadFileExample()
        {
            var request:URLRequest = new URLRequest();
            request.url = "http://www.[yourdomain].com/downloadfile.cfm";
            request.method = URLRequestMethod.GET;
            request.data = new URLVariables("id=2");
            fileToDownload = new FileReference();
            try
            {
                fileToDownload.download(request, "file2.txt");
            }
            catch (error:Error)
            {
                trace("Unable to download file.");
            }
        }
    }
}


The following code demonstrates the ColdFusion script, download.cfm, that downloads one of two files from the server, depending on the value of a URL variable:

<cfparam name="URL.id" default="1" />
<cfswitch expression="#URL.id#">
    <cfcase value="2">
        <cfcontent type="text/plain" file="#ExpandPath('two.txt')#" deletefile="No" />
    </cfcase>
    <cfdefaultcase>
        <cfcontent type="text/plain" file="#ExpandPath('one.txt')#" deletefile="No" />
    </cfdefaultcase>
</cfswitch>