, когда я пытаюсь скачать большой файл, а затем записать его в двоичный файл, появляется сообщение об ошибке
"номер плохой записи"
в этой строке Put #fileNo, , WinHttpReq.responseBody
Я думаю, это потому, что номер записи имеет тип данных Long, как он говорит здесь он не появляется, когда файл маленький, даже если он большой, он пишет без проблем, только последний кусок, так что я могу делать?
Function DownloadFile(ByVal url As String, ByVal Path As String, ByVal UserName As String, ByVal Password As String) As Boolean
DownloadFile = False
Dim chunkSize As Long
chunkSize = 500000000 '500 mega
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
Dim WinHttpReq As Object
Dim totalSize As Double
Dim currentStartByte As Double
Dim currentEndByte As Double
Dim fileNo As Integer
Dim lastByte As Double
'delete the file
DeleteFile (Path)
Sleep (60000)
'get the total file size
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
WinHttpReq.Open "HEAD", url, False
WinHttpReq.SetCredentials UserName, Password, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
WinHttpReq.setRequestHeader "User-Agent", 0
WinHttpReq.send
totalSize = WinHttpReq.getResponseHeader("Content-Length")
Set WinHttpReq = Nothing
'set the initial start and end byte
currentStartByte = 0
If totalSize < chunkSize Then
currentEndByte = totalSize
Else
currentEndByte = chunkSize
End If
Do While (currentEndByte > currentStartByte)
'read the chunked data from the responseBody
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
WinHttpReq.Open "GET", url, False
WinHttpReq.SetCredentials UserName, Password, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
WinHttpReq.setRequestHeader "User-Agent", 0
WinHttpReq.setRequestHeader "Range", "bytes=" + Str(currentStartByte) + "-" + Str(currentEndByte)
WinHttpReq.send
'open binary file
fileNo = FreeFile
Open Path For Binary As #fileNo
'Set pointer to end of file
Seek #fileNo, LOF(fileNo) + 1
Put #fileNo, , WinHttpReq.responseBody
'set the start and end byte for the next loop
currentStartByte = currentStartByte + chunkSize + 1
currentEndByte = currentEndByte + chunkSize + 1
'if the remaining byte less than chunk size
If currentEndByte > totalSize Then
currentEndByte = totalSize
End If
Set WinHttpReq = Nothing
Close #fileNo
Loop
Set WinHttpReq = Nothing
DownloadFile = True
End Function