Resume a downloaded file

Just starting out? Need help? Post your questions and find answers here.
atomo
User
User
Posts: 65
Joined: Thu May 22, 2008 10:32 pm

Resume a downloaded file

Post by atomo »

Hi,
I try to resume a downloaded file, it works with this code but it take a very long time to initialise, i don't know why.
Start the program then wait a moment like 30% and restart.
Thanks to help me :)

Code: Select all

Enumeration
  #Window_0
  #pourcentage
  #ProgressBar_0
  #Boutton_0
EndEnumeration

Global Quit.b
Global Close.b

Procedure update()
  DownloadURL.s = "http://www.purebasic.com/download/PureBasic_Demo.exe"
  DownloadFilename.s= "PureBasic_Demo.exe"
    
  Protected hInet, hURL, Bytes 
  Protected BufferLength = 2048, Buffer.s = Space(BufferLength) 
    
  Protected Url.s      = DownloadURL.s 
  Protected Filename.s = DownloadFilename.s 
  Protected File 
  Protected CurrentSize, PreviousSize, FileSize, time, BytesPerSecond 
  Protected Domain.s, String.s, i, BufferLengthWas = BufferLength 
  Protected hInetCon, hHttpOpenRequest, iretval 
    
  hInet = InternetOpen_("Downloader",0,0,0,0) 
  hURL  = InternetOpenUrl_(hInet,Url.s,0,0,$80000000,0) 
    
  ;Get filesize 
  Domain.s  = StringField(Url.s,3,"/") 
  hInetCon = InternetConnect_(hInet,Domain.s,80,#Null,#Null,3,0,0) 
  If hInetCon 
    hHttpOpenRequest = HttpOpenRequest_(hInetCon,"HEAD",ReplaceString(Url.s,"http://"+Domain.s+"/",""),#Null,#Null,0,$80000000,0) 
    If hHttpOpenRequest 
      iretval = HttpSendRequest_(hHttpOpenRequest,#Null,0,0,0) 
      If iretval 
        HttpQueryInfo_(hHttpOpenRequest,19,@Buffer.s,@BufferLength,0) ;changes the buffer length 
        String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas 
        If Trim(String.s) = "200" 
          HttpQueryInfo_(hHttpOpenRequest,22,@Buffer.s,@BufferLength,0) 
          String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas 
          If FindString(String.s,"Content-Length:",1) 
            i = FindString(String.s,"Content-Length:",1) + Len("Content-Length:") 
            String.s = Mid(String.s,i,Len(String.s)-i) 
            FileSize = Val(Trim(String.s))/1024
          EndIf 
        EndIf 
      EndIf 
    EndIf 
  EndIf 
  
  ;Download file and update status 
  If hURL 
    taille_fichier = FileSize("PureBasic_Demo.exe")
    If taille_fichier > 0 ;le fichier existe déjà alors reprise
      CurrentSize = taille_fichier
      File = OpenFile(#PB_Any, "PureBasic_Demo.exe") ;ouverture du fichier
      FileSeek(File, CurrentSize)
      InternetSetFilePointer_(hURL, CurrentSize, 0, #FILE_BEGIN ,0)
    Else ;nouveau téléchargement
      File = CreateFile(#PB_Any,Filename.s)
    EndIf
    If File 
      time = ElapsedMilliseconds()
      While InternetReadFile_(hURL,@Buffer.s,BufferLength,@Bytes) And Bytes > 0
        If Quit = #True
          Break
        EndIf
        WriteData(File,@Buffer.s,Bytes) 
        CurrentSize + Bytes
        Pourcentage.f = ((CurrentSize/1024)/FileSize)*100
        SetGadgetState(#ProgressBar_0, Pourcentage)
        If time < ElapsedMilliseconds() - 1000 
          time = ElapsedMilliseconds() 
          BytesPerSecond = CurrentSize - PreviousSize 
          PreviousSize = CurrentSize 
        EndIf
      Wend 
      CloseFile(File) 
    EndIf
  EndIf
  InternetCloseHandle_(hURL) 
  InternetCloseHandle_(hInetCon) 
  InternetCloseHandle_(hInet)
  Close = #True
  EndProcedure
  
If OpenWindow(#Window_0, 0, 0, 250, 105, "Download", #PB_Window_ScreenCentered)
  If CreateGadgetList(WindowID(#Window_0))
    ProgressBarGadget(#ProgressBar_0, 21, 30, 207, 20, 0, 100)
    ButtonGadget(#Boutton_0, 95, 77, 60, 20, "Abord")
    
  EndIf
EndIf

CreateThread(@update(), 0)

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget ;si on est dans un gadget
      Select EventGadget()
        Case #Boutton_0
          Quit = #True
      EndSelect
  EndSelect
Until Close = #True
End
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

hi, i answer you on the french forum ;)
http://www.purebasic.fr/french/viewtopi ... 2417#82417
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Resume a downloaded file (Win API only)

Post by jassing »

I know it's an old thread -- but I had had a project that referenced this code -- the InternetSetFilePointer_ basically took as long as just re-downloading the file.
To solve the issue, here's what I did:

Changed this code:

Code: Select all

If InternetSetFilePointer_(hURL, CurrentSize, 0, #FILE_BEGIN ,0) <> CurrentSize
  MessageRequester("Erreur", "Echec de InternetSetFilePointer_()", #MB_ICONERROR)
EndIf
to this:

Code: Select all

#HTTP_ADDREQ_FLAG_ADD_IF_NEW = $10000000
cHeader.s = "Range: bytes="+Str(Lof(file))+"-"
if HttpAddRequestHeaders_(hURL,cHeader,Len(cHeader),#HTTP_ADDREQ_FLAG_ADD_IF_NEW)
  ; Success!
else ; Failure
endif
Now the 'resume' is much faster....
Hope that helps anyone else looking to do resumes.
Post Reply