Search code examples
apache-flexactionscript-3

How can I get the size of a remote file before I start downloading it?


I need to figure out the size of a remote file before I download it.And I know that in local place this can be done as follows

var _filePath:String = "X:/save/abc.exe";
var file:File = new File(_filePath);
if (file.size > 1000)
{
    trace("too large");
}

But when I tried a remote file like

var _filePath:String = "http://www.aaa.com/webfile.exe";

It didn't work.And how can I get the size of a remote file before I use

  var _downLoadPath:String = "http://www.aaa.com/webfile.exe";

Thanks!


Solution

  • What I have in mind now is to use the progress event like this:

    var downloadURL:URLRequest; var fileName:String = "SomeFile.pdf"; var file:FileReference;

    function FileReference_event_progress()
    {
        downloadURL = new URLRequest();
        downloadURL.url = "http://www.aaa.com/webfile.exe";
        file = new FileReference();
        file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        file.download(downloadURL, fileName);
    }
    
    function progressHandler(event:ProgressEvent):void
    {
        var file:FileReference = FileReference(event.target);
        trace(event.bytesTotal);
    }
    

    This script will output the total size of the file (but will output it after the download process is started). What you can do is stop download immediately after the first piece of the file is received together with the total size - this is what I have like a workaround.

    Another thing you can try is to use trace(file.bytesTotal); instead of file.download(downloadURL, fileName); but I'm not sure it will work without the process of download is started as there is no request send to the file.