Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; #FUNCTION# ====================================================================================================================
- ; Name...........: _FTP_ProgressDownload
- ; Description ...: Downloads a file in Binary Mode and shows a Progress window or by Calling a User defined Function.
- ; Syntax.........: _FTP_ProgressDownload($l_FTPSession, $s_LocalFile, $s_RemoteFile[, $FunctionToCall = ""])
- ; Parameters ....: $l_FTPSession - as returned by _FTP_Connect().
- ; $s_LocalFile - The local file to create.
- ; $s_RemoteFile - The remote source file.
- ; $FunctionToCall - [Optional] A function which can update a Progressbar and
- ; react on UserInput like Click on Abort or Close App.
- ; (More info in the end of this comment)
- ; Return values .: Success - 1
- ; Error: 0 and @error:
- ; -1 -> Local file couldn't be created
- ; -2 -> Unable to get RemoteFile size
- ; -3 -> Open RemoteFile failed
- ; -4 -> Read from Remotefile failed
- ; -5 -> Close RemoteFile failed
- ; -6 -> Download aborted by PercentageFunc, Return of Called Function
- ; -7 -> Local file write failed
- ; Author ........: limette, Prog@ndy, James (autoit.de)
- ; Modified.......: jchd
- ; Remarks .......:
- ; Information about $FunctionToCall:
- ; Parameter: $Percentage - The Percentage of Progress
- ; Return Values: Continue Download - 1
- ; Abort Download - 0 Or negative
- ; These Return Values are returned by _FTP_ProgressDownload(), too,
- ; so you can react on different Actions like Aborting by User, closing App or TimeOut of whatever
- ;~ Examples:
- ;~ Func _UpdateProgress($Percentage)
- ;~ ProgressSet($percent,$percent &"%")
- ;~ If _IsPressed("77") Then Return 0 ; Abort on F8
- ;~ Return 1 ; bei 1 Fortsetzten
- ;~ Endfunc
- ;
- ;~ Func _UpdateProgress($Percentage)
- ;~ GUICtrlSetData($ProgressBarCtrl,$percent)
- ;~ Switch GUIGetMsg()
- ;~ Case $GUI_EVENT_CLOSE
- ;~ Return -1 ; _FTP_DownloadProgress Aborts with -1, so you can exit you app afterwards
- ;~ Case $Btn_Cancel
- ;~ Return 0 ; Just Cancel, without special Return value
- ;~ EndSwitch
- ;~ Return 1 ; Otherwise contine Download
- ;~ Endfunc
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func _FTP_ProgressDownload($l_FTPSession, $s_LocalFile, $s_RemoteFile, $FunctionToCall = "")
- If $__ghWinInet_FTP = -1 Then Return SetError(-2, 0, 0)
- Local $fhandle = FileOpen($s_LocalFile, 18)
- If $fhandle < 0 Then Return SetError(-1, 0, 0)
- Local $ai_ftpopenfile = DllCall($__ghWinInet_FTP, 'handle', 'FtpOpenFileW', 'handle', $l_FTPSession, 'wstr', $s_RemoteFile, 'dword', $GENERIC_READ, 'dword', $FTP_TRANSFER_TYPE_BINARY, 'dword_ptr', 0)
- If @error Or $ai_ftpopenfile[0] = 0 Then
- FileClose($fhandle) ; <-- !!!
- Return SetError(-3, _WinAPI_GetLastError(), 0)
- EndIf
- Local $ai_FTPGetFileSize = DllCall($__ghWinInet_FTP, 'dword', 'FtpGetFileSize', 'handle', $ai_ftpopenfile[0], 'dword*', 0)
- If @error Then
- FileClose($fhandle) ; <-- !!!
- Return SetError(-2, _WinAPI_GetLastError(), 0)
- EndIf
- If $FunctionToCall = "" Then ProgressOn("FTP Download", "Downloading " & $s_LocalFile)
- Local $glen = _WinAPI_MakeQWord($ai_FTPGetFileSize[0], $ai_FTPGetFileSize[2]) ;FileGetSize($s_RemoteFile)
- Local Const $ChunkSize = 256 * 1024
- Local $last = Mod($glen, $ChunkSize)
- Local $parts = Ceiling($glen / $ChunkSize)
- Local $buffer = DllStructCreate("byte[" & $ChunkSize & "]")
- Local $ai_InternetCloseHandle, $ai_FTPread, $out, $ret, $lasterror
- Local $x = $ChunkSize
- Local $done = 0
- For $i = 1 To $parts
- If $i = $parts And $last > 0 Then
- $x = $last
- EndIf
- $ai_FTPread = DllCall($__ghWinInet_FTP, 'bool', 'InternetReadFile', 'handle', $ai_ftpopenfile[0], 'struct*', $buffer, 'dword', $x, 'dword*', $out)
- If @error Or $ai_FTPread[0] = 0 Then
- $lasterror = _WinAPI_GetLastError()
- $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
- ; No need to test @error.
- FileClose($fhandle)
- If $FunctionToCall = "" Then ProgressOff()
- Return SetError(-4, $lasterror, 0)
- EndIf
- $ret = FileWrite($fhandle, BinaryMid(DllStructGetData($buffer, 1), 1, $ai_FTPread[4]))
- If Not $ret Then
- $lasterror = _WinAPI_GetLastError()
- $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
- ; No need to test @error.
- FileClose($fhandle)
- FileDelete($s_LocalFile)
- If $FunctionToCall = "" Then ProgressOff()
- Return SetError(-7, $lasterror, 0)
- EndIf
- $done += $ai_FTPread[4]
- If $FunctionToCall = "" Then
- ProgressSet(($done / $glen) * 100)
- Else
- $ret = Call($FunctionToCall, ($done / $glen) * 100)
- If $ret <= 0 Then
- $lasterror = @error
- $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
- ; No need to test @error.
- FileClose($fhandle)
- FileDelete($s_LocalFile)
- If $FunctionToCall = "" Then ProgressOff()
- Return SetError(-6, $lasterror, $ret)
- EndIf
- EndIf
- Sleep(10)
- Next
- FileClose($fhandle)
- If $FunctionToCall = "" Then ProgressOff()
- $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
- If @error Or $ai_InternetCloseHandle[0] = 0 Then
- Return SetError(-5, _WinAPI_GetLastError(), 0)
- EndIf
- Return 1
- EndFunc ;==>_FTP_ProgressDownload
Add Comment
Please, Sign In to add comment