Jump to content

how can (Stop and resume) for download


Recommended Posts

Have a look at get the idea of how to monitor when a button is pressed to stop the download.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

OK, the clues were there with stopping (not resuming) a download. Look at InetGet() Example and the background parameter, if this is 1 then we can monitor the download using InetGetInfo() whilst in a loop, also we can monitor GUIGetMsg() too. Then if GUIGetMsg() equals the "Cancel" Button we Exit the Loop and close the download (InetClose())

From my Example this is how I do it >>

While InetGetInfo($hDownload, 2) = 0
        Switch GUIGetMsg() ; If the GUIMsg() is the same as the Button "Cancel" then Exit the Loop and close the download.
            Case -3, $iButton
                GUICtrlSetData($iLabel, "Download Cancelled!")
                $iError = 1
                ExitLoop
        EndSwitch

        $iBytesRead = InetGetInfo($hDownload, 0)
        $iPercentage = $iBytesRead * 100 / $iFileSize
        $sProgressText = "Downloading " & __ByteSuffix($iBytesRead) & " Of " & __ByteSuffix($iFileSize)
        GUICtrlSetData($iLabel, $sProgressText)
        GUICtrlSetData($iProgress, $iPercentage)
        Sleep(100)
WEnd
Otherwise post the code you've tried. Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

OK.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I knew it was possible to request a specific part of a file using the Range header, but I never tried it, so I decided to check it out.

While my example is very rough and has only minimal errorchecking it seems to work ok with most files I tested it on.

If you point this example to an existing destingation file, it will assume that this is a continuation of a previous download. And request to resume the download from the last downloaded byte. This means you can continue downloading the same file from a different location, but it also means that you can really mess up the destination file if it's not the same as what you are trying to download.

If the file if 800 Kb it will request the download to start at the 801 byte untill complete.

It can be made to handle multiple simultanious downloads.

I wouldn't advise using the code as is, but it should allow you to come up with something yourself.

#include "WinHttp.au3"
#include <GUIConstantsEx.au3>

Global $sBigFileUrl = ;Find the url to reasonably large file here. I googled a big picture for testing, which allowed me to open the incomplete file, see a part missing, finish the download and get complete picture as a result.
Global $sBigFileDest = @ScriptDir & "\Bigfile.jpg"
Global $aDownload[3]

GUICreate("Test",500,300)
Global $GuiCtrlButtonStart = GUICtrlCreateButton("Start",10,10,200)
Global $GuiCtrlButtonStop = GUICtrlCreateButton("Stop",250,10,200)
GUICtrlSetState($GuiCtrlButtonStop, $GUI_DISABLE)
Global $GuiCtrlEdit = GUICtrlCreateEdit("",10,40,450,250)
GUISetState()

Global $hOpen = _WinHttpOpen()
While 1
    Switch GUIGetMsg()
        Case -3
            _WinHttpCloseHandle($hOpen)
            Exit
        Case $GuiCtrlButtonStart
            $aDownload = _StartDownload($sBigFileUrl, $sBigFileDest)
            If @error Then
                _EditWrite("Couldn't start/resume download")
            Else
                _EditWrite("Download started")
            EndIf
        Case $GuiCtrlButtonStop
            _EditWrite("Download interrupted")
            _StopDownload($aDownload)
    EndSwitch
    If $aDownload[1] Then _AppendToFile($aDownload)
WEnd

Func _StartDownload($sUrl, $sDest)
    Local $aReturn[3]
    Local $aUrl = _WinHttpCrackUrl($sURL)
    If @error Then Return SetError(1,0,$aReturn)
    Local $hConnect = _WinHttpConnect($hOpen, $aUrl[2])
    If @error Then Return SetError(2,0,$aReturn)
    Local $hRequest = _WinHttpOpenRequest($hConnect,"GET",$aUrl[6] & $aUrl[7])
    If @error Then
        _WinHttpCloseHandle($hConnect)
        Return SetError(3,0,$aReturn)
    EndIf
    If FileExists($sDest) Then
        _WinHttpAddRequestHeaders($hRequest, "Range: bytes=" & FileGetSize($sDest) & "-") ;I used FileSize+1 before, but it distorted the picture.
        _EditWrite("There seems to be " & FileGetSize($sDest) & "kb downloaded already")
    Else
        _EditWrite("Creating a new file")
    EndIf
    _WinHttpSendRequest($hRequest)
    If @error Then
        _WinHttpCloseHandle($hConnect)
        Return SetError(4,0,$aReturn)
    EndIf
    _WinHttpReceiveResponse($hRequest)
    If @error Then
        _WinHttpCloseHandle($hConnect)
        Return SetError(5,0,$aReturn)
    EndIf
    If StringInStr(_WinHttpQueryHeaders($hRequest),"HTTP/1.1 416 Requested Range Not Satisfiable") Then
        _WinHttpCloseHandle($hConnect)
        Return SetError(6,0,$aReturn)
    EndIf
    $aReturn[0] = $hConnect
    $aReturn[1] = $hRequest
    $aReturn[2] = FileOpen($sDest,17)
    GUICtrlSetState($GuiCtrlButtonStop, $GUI_ENABLE)
    GUICtrlSetState($GuiCtrlButtonStart, $GUI_DISABLE)
    Return $aReturn
EndFunc

Func _AppendToFile(ByRef $aDownload)
    Local $bChunk = _WinHttpReadData($aDownload[1],2)
    If @error Then
        _EditWrite("Download complete")
        _StopDownload($aDownload)
        Return
    EndIf
    FileWrite($aDownload[2], $bChunk)
EndFunc

Func _StopDownload(ByRef $aDownload)
    _WinHttpCloseHandle($aDownload[0])
    _WinHttpCloseHandle($aDownload[1])
    FileClose($aDownload[2])
    $aDownload[0] = False
    $aDownload[1] = False
    $aDownload[2] = False
    GUICtrlSetState($GuiCtrlButtonStop, $GUI_DISABLE)
    GUICtrlSetState($GuiCtrlButtonStart, $GUI_ENABLE)
    _EditWrite("Downloaded " & FileGetSize($sBigFileDest) & "kb to file")
    _EditWrite("Download stopped")
    Return
EndFunc

Func _EditWrite($sMsg)
    GUICtrlSetData($GuiCtrlEdit, GUICtrlRead($GuiCtrlEdit) & @CRLF & $sMsg)
EndFunc

P.S. The range header actually allows you to request any range of bytes you desire, so it could even be used to split your download in blocks and download all blocks from different locations, putting the file together as you go. P2P style. I doubt it's worth the hassle though.

Link to comment
Share on other sites

thank you

but that example doesn't sport FTP Upload and HTTP Upload

I don't know about FTP, but I'm pretty sure resuming an interrupted HTTP upload is going to be hard to do.

your example mean you can't resume after script exit

It most certainly does. You could even resume the download on another PC if you wanted.

I had no intention of making a solution that would work out of the box, but as an example on resuming a HTTP download I think it works.

Edit:

I can download and pause this file over FTP without trouble and the resulting file is valid.

Creating a session might be tricky and I am discarding any username/password in the url after _WinHttpCrackUrl(), but it's something.

Edited by Tvern
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...