The command CopyFile() can download a file from the internet and copy it to the user's hard drive. I want to write a new copyfile function with a callback, so that the download progress can be displayed:
The problem is that a URL opened with ReadStream() has an unknown size. If I pre-cache the file with ReadFile(), the entire file is loaded, so I know the size, but I can't display the progress until after the actual file data is already downloaded.
How can I find out the size of a file on the internet, or a stream opened with ReadStream()?
Import leadwerks.lwmath Function CopyFile2:Int(src$,dst$="",callback:Byte Ptr=Null) src$=Replace(src,"","/") src=Replace(src,"://","::") If dst="" dst=AppDir$+"/"+StripDir(src) If callback=Null Return CopyFile(src,dst) Else Local DownloadProgressCallback(p#) DownloadProgressCallback=callback out:TStream=WriteFile(dst) If Not out Return in:TStream=ReadFile(src) If Not in out.close() Return EndIf size=in.size() While Not Eof(in) WriteByte out,ReadByte(in) newprogress#=Float(in.pos())/Float(in.size()) 'If Round(newprogress*100.0)>Round(progress#*100.0) ' progress=newprogress DownloadProgressCallback newprogress 'EndIf Wend in.close() out.close() Return True EndIf EndFunction win:TGadget=CreateWindow("",200,200,400,300,Null,WINDOW_TITLEBAR) Global progbar:TGadget=CreateProgBar(10,10,win.clientwidth()-20,28,win) CopyFile2("http://www.leadwerks.com/index.php","",DLCallBack) Function DLCallBack(progress:Float) UpdateProgBar progbar,progress If PeekEvent() WaitEvent() EndFunction
The problem is that a URL opened with ReadStream() has an unknown size. If I pre-cache the file with ReadFile(), the entire file is loaded, so I know the size, but I can't display the progress until after the actual file data is already downloaded.
How can I find out the size of a file on the internet, or a stream opened with ReadStream()?