AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How Can I Make A Download With ProgressBar?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
KorsaNNeTTeaM



Joined: 14 Dec 2007
Posts: 2

PostPosted: Fri Dec 14, 2007 11:42 pm    Post subject: How Can I Make A Download With ProgressBar? Reply with quote

Hi everyone. Firstly my english is bad. So excuse me.

I have some problem. I am new in autohotkey. I want to download a file on internet. And, I want to show it a progress bar.

For example;

Download this file.
http://www.autohotkey.com/download/AutoHotkeyInstall.exe

Code:
url=http://www.autohotkey.com/download/AutoHotkeyInstall.exe
save=C:\Program Files\Auto Hotkey\AutoHotkeyInstall.exe
UrlDownloadToFile, %url%, %save%


There is no problem there Smile

Now I want to show this downloading in progress bar. The code here:

Code:
Gui, Add, Progress, w300
Gui, Add, Text,, Downloading:
Gui, Show, , Downloading...


now, what can I do? how can I learn downloading file size and how can I learn downloaded file size. And how can I show these in progressbar.

If you help me, ı am to be very happy. Thanks all.
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 906

PostPosted: Sat Dec 15, 2007 1:33 am    Post subject: Reply with quote

See if this is helpful:
http://www.autohotkey.com/forum/viewtopic.php?t=11398
Back to top
View user's profile Send private message
DerRaphael



Joined: 23 Nov 2007
Posts: 604
Location: 127.0.0.1

PostPosted: Sat Dec 15, 2007 1:42 am    Post subject: Reply with quote

This is a rather simple file downloader. It uses Connectioncheck to desired download server and file integrity check by calculating the MD5 hash

Code:

; Simple File Downloader with connection and integrity check
; by derRaphael

fileName := "AutoHotkey104705_Install.exe"
totalFileSize := 2016668

if !(FileExist(fileName)) {
   Message := "The desired file " fileName " `nwas not found in the current directory`n"
   Message .= "It will be downloaded from the internet`n`n"
   Message .= "Press OK to start the download"

   MsgBox,65,For your interest, %Message%

   IfMsgBox, OK
   {
      If InternetCheckConnection("http://www.autohotkey.com") {
         msg := "Please wait while download is in progress"
         ; Show splash bar
         Progress, 0 FM10 FS8 WM400 WS400 ,`n,%msg%, Downloading %fileName%, Tahoma
         ; Now set the splash bar updating
         SetTimer, uProgress, 250
         ; start the download
         UrlDownloadToFile, http://www.autohotkey.com/download/AutoHotkeyInstall.exe, %fileName%
         ; download Finished turn off splashbar
         SetTimer, uProgress, off
         Progress, Off

         ; Verify Download
         desiredMD5Hash := "3cc3d81ed9ee02698006e63f6ee83a38"   ; This is our known MD5 Hash
         FileGetSize, DataLength, %fileName%                    ; FileSize - used for Hash Function
         FileRead   , BinaryData, %fileName%                    ; BinaryData - to calculate the MD5
         actualMD5Hash  := HASH( BinaryData, DataLength , 3 )   ; Calculate the Hash
         if (desiredMD5Hash=actualMD5Hash) {
            MsgBox,65,Message, Download was successful
         } else {
            MsgBox,65,Message, Sorry, but download failed.
         }
      } else {
         Msgbox, 48, Error, Your Internetlink seems to be down.`nI can't download %fileName%.
      }
   } else {
      ExitApp
   }
}
Return

uProgress:
   ; get filesize
   FileGetSize, fs, %fileName%
   ; calculate percent
   a := Floor(fs/totalFileSize * 100)
   ; calculate percent with two
   b := Floor(fs/totalFileSize * 10000)/100
   SetFormat, float, 0.2
   b += 0
   Progress, %a%, %b%`% done (%fs% Bytes of %totalFileSize% Bytes)
return

InternetCheckConnection(Url="",FIFC=1) {
;  SKAN: http://www.autohotkey.com/forum/viewtopic.php?p=60892#60892
   Return DllCall("Wininet.dll\InternetCheckConnectionA", Str,Url, Int,FIFC, Int,0)
}

HASH(ByRef sData, nLen, SID = 3) { ; SID = 3: MD5, 4: SHA1
;  Laszlo: http://www.autohotkey.com/forum/viewtopic.php?p=113252#113252
   DllCall("advapi32\CryptAcquireContextA", UIntP,hProv, UInt,0, UInt,0, UInt,1, UInt,0xF0000000)
   DllCall("advapi32\CryptCreateHash", UInt,hProv, UInt,0x8000|0|SID, UInt,0, UInt,0, UIntP, hHash)

   DllCall("advapi32\CryptHashData", UInt,hHash, UInt,&sData, UInt,nLen, UInt,0)

   DllCall("advapi32\CryptGetHashParam", UInt,hHash, UInt,2, UInt,0, UIntP,nSize, UInt,0)
   VarSetCapacity(HashVal, nSize, 0)
   DllCall("advapi32\CryptGetHashParam", UInt,hHash, UInt,2, UInt,&HashVal, UIntP,nSize, UInt,0)

   DllCall("advapi32\CryptDestroyHash", UInt,hHash)
   DllCall("advapi32\CryptReleaseContext", UInt,hProv, UInt,0)

   IFormat := A_FormatInteger
   SetFormat Integer, H
   Loop %nSize%
      sHash .= SubStr(*(&HashVal+A_Index-1)+0x100,-1)
   SetFormat Integer, %IFormat%
   Return sHash
}




I'll wrap it as a function, so it might be used more generic and not that user specific. But it'll help in your case anyways Smile


greetings
derRaphael
Back to top
View user's profile Send private message
KorsaNNeTTeaM



Joined: 14 Dec 2007
Posts: 2

PostPosted: Sat Dec 15, 2007 4:52 pm    Post subject: Reply with quote

Thanks DerRaphael. Now, my problem is solved. I undestood to method. thanks again.
Back to top
View user's profile Send private message
Loriss



Joined: 26 Jul 2004
Posts: 64

PostPosted: Sun Dec 16, 2007 9:41 pm    Post subject: Only small adjustments. Reply with quote

Only small adjustments,For general use with total filesize from
HTTQueryInfo funcion.
(When the file is not found, it creates a file of 1638 bytes).

Code:
fileName = AutoHotkey104705_Install.exe
url = http://www.autohotkey.com/download/AutoHotkeyInstall.exe
saveDir = %A_mydocuments%
Progress, X0 Y63 h75 w430,, %url% , Download , bold
         SetTimer, uProgress, 150
         totalFileSize := HttpQueryInfo(url, 5)
         UrlDownloadToFile, %url%, %fileName%
         SetTimer, uProgress, off
         Progress, Off
         FileMove,%fileName%,%saveDir%
         filegetSize,sizefil,%A_mydocuments%\%fileName%
         if(sizefil = 1638)
         goto,nofile
         msgbox,Download finished
         exitapp

nofile:          ; When the file is not found, it creates a file of 1638 bytes 
{
filedelete,%A_mydocuments%\%fileName%
msgbox,File not found on the site.
}
Exitapp

uProgress:
   FileGetSize, fs, %fileName% ,K
   a := Floor(fs/totalFileSize * 100*1024)
   b := Floor(fs/totalFileSize * 10000*1024)/100
   SetFormat, float, 0.2
   b += 0
TFileSize := totalFileSize / 1024
Tfs=%TFileSize%
Progress,%a%,%b%`%        downloaded: %fs% Kb.    of     %Tfs% Kb.
return

HttpQueryInfo(URL, QueryInfoFlag=21, Proxy="", ProxyBypass="") {
hModule := DllCall("LoadLibrary", "str", "wininet.dll")
If (Proxy != "")
AccessType=3
Else
AccessType=1
io_hInternet := DllCall("wininet\InternetOpenA"
, "str", "" ;lpszAgent
, "uint", AccessType
, "str", Proxy
, "str", ProxyBypass
, "uint", 0) ;dwFlags
If (ErrorLevel != 0 or io_hInternet = 0) {
DllCall("FreeLibrary", "uint", hModule)
return, -1
}
iou_hInternet := DllCall("wininet\InternetOpenUrlA"
, "uint", io_hInternet
, "str", url
, "str", "" ;lpszHeaders
, "uint", 0 ;dwHeadersLength
, "uint", 0x80000000 ;dwFlags: INTERNET_FLAG_RELOAD = 0x80000000 // retrieve the original item
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or iou_hInternet = 0) {
DllCall("FreeLibrary", "uint", hModule)
return, -1
}
VarSetCapacity(buffer, 1024, 0)
VarSetCapacity(buffer_len, 4, 0)
Loop, 5
{
  hqi := DllCall("wininet\HttpQueryInfoA"
  , "uint", iou_hInternet
  , "uint", QueryInfoFlag ;dwInfoLevel
  , "uint", &buffer
  , "uint", &buffer_len
  , "uint", 0) ;lpdwIndex
  If (hqi = 1) {
    hqi=success
    break
  }
}
IfNotEqual, hqi, success, SetEnv, res, timeout
If (hqi = "success") {
p := &buffer
Loop
{
  l := DllCall("lstrlen", "UInt", p)
  VarSetCapacity(tmp_var, l+1, 0)
  DllCall("lstrcpy", "Str", tmp_var, "UInt", p)
  p += l + 1 
  res := res  . "`n" . tmp_var
  If (*p = 0)
  Break
}
StringTrimLeft, res, res, 1
}
DllCall("wininet\InternetCloseHandle",  "uint", iou_hInternet)
DllCall("wininet\InternetCloseHandle",  "uint", io_hInternet)
DllCall("FreeLibrary", "uint", hModule)
return, res
}


The progress bar now has almost one look and working acceptable.
But we would like to see all the progress bar really
driven from hasc of bit really downloaded, bit after bit.
For that we expect the help of friends most experts.
We all are waiting any posibile improvement.
Thanks.


P.S.
at this tread (SKAN): http://www.autohotkey.com/forum/viewtopic.php?p=118074#118074
There is a script that use the methods of donloading Windows very professional, but is not direct and immediate.
We must give him the location and confirmation,
And thing most importat is that if you use url not standard, type (http://web.tiscali.it/paologweb/), (free sites), does not accept the url and therefore it does not work.
_________________
Loriss
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group