开发者

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

开发者 https://www.devze.com 2022-12-24 19:43 出处:网络
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

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");
}
开发者_C百科

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!


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.


I have never used Action Script, but as it appears you are downloading the file via http what you are looking for is the content_length in the http header. Http does have a HEAD request as well as the more well known GET and POST. If you can figure out how too make a HEAD request, then you can parse the response header for content length.

0

精彩评论

暂无评论...
验证码 换一张
取 消