how can i get a % of download from ReceiveHTTPFile ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 782
Joined: Fri Dec 04, 2015 9:26 pm

how can i get a % of download from ReceiveHTTPFile ?

Post by skinkairewalker »

hello everyone !!

i am downloading a file using ReceiveHTTPFile , but I'd like to know how many% of the download has already been downloaded ...
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: how can i get a % of download from ReceiveHTTPFile ?

Post by Dude »

The manual says HTTPProgress() can be used to get the number of bytes currently transferred (see https://www.purebasic.com/documentation ... gress.html), but I don't know how to get the file size of the target file to work out the percentage of the bytes. I'm sure there'd be a tip in these forums that shows how.
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: how can i get a % of download from ReceiveHTTPFile ?

Post by kenmo »

A quick way to get file size, usually works:

Code: Select all

Procedure.i HTTPFileSize(URL.s)
  Protected Result.i = -1
  If (URL)
    Protected Raw.s = GetHTTPHeader(URL)
    Protected i.i = FindString(Raw, "Content-Length: ")
    Protected j.i = FindString(Raw, #CRLF$, i)
    If (i And j)
      Result = Val(Mid(Raw, i + Len("Content-Length: "), j - i - Len("Content-Length: ")))
    EndIf
  EndIf
  ProcedureReturn (Result)
EndProcedure

InitNetwork()
Debug HTTPFileSize("https://www.purebasic.fr/english/styles/SkyLineBlue/imageset/purebasic_logo.png")
Then of course, % = 100.0 * BytesDownloaded / FileSize
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 782
Joined: Fri Dec 04, 2015 9:26 pm

Re: how can i get a % of download from ReceiveHTTPFile ?

Post by skinkairewalker »

thanks you everyone !!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: how can i get a % of download from ReceiveHTTPFile ?

Post by Dude »

Good code, Kenmo. :) I tried it with your avatar and it correctly returned 370 bytes:

Code: Select all

Debug HTTPFileSize("https://www.purebasic.fr/english/download/file.php?avatar=782_1286934356.png")
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: how can i get a % of download from ReceiveHTTPFile ?

Post by djes »

I think this info should be returned somehow by ReceiveHTTPFile() to avoid duplicate http request.
nalor
Enthusiast
Enthusiast
Posts: 121
Joined: Thu Apr 02, 2009 9:48 pm

Re: how can i get a % of download from ReceiveHTTPFile ?

Post by nalor »

djes wrote:I think this info should be returned somehow by ReceiveHTTPFile() to avoid duplicate http request.
I fully agree - any ideas how to access the header fields of the transfer started by ReceiveHTTPFile ?
Post Reply