vb通过FTP下载exe,rar等文件的问题
请先下载这个http://www.mndsoft.com/blog/article.asp?id=1117
问题:
如何可以在改成二进制下载后,进度条同样有作用?
现在是下载txt文件没有问题,但是exe\rar\zip就不行,如何可以实现ftp下载exe\rar\zip同样可以显示真实的进度条?
有朋友说是这段代码的问题
Public Function GetFiles(strFile As String, strNewFile As String, lngFileSize As Long, vMode As Integer) As Boolean
Dim hFile As Long
Dim sBuffer As String
Dim sReadBuffer As String * 2048
Dim lNumberOfBytesRead As Long
Dim bDoLoop As Boolean
Dim Sum As Long
Dim x As Integer
GetFiles = True
If vMode = 0 Then '
Transfer = FTP_TRANSFER_TYPE_ASCII
Else
Transfer = FTP_TRANSFER_TYPE_BINARY
End If
InProgress = True
hFile = FtpOpenFile(hConnection, Trim(strFile), GENERIC_READ, Transfer, 0)
If hFile = 0 Then
MsgBox Err.LastDllError, vbCritical + vbOKOnly, "提示"
GetFiles = False
Exit Function
End If
Open strNewFile For Binary Access Write As #2
bDoLoop = True
StopTransfert = False
While bDoLoop
DoEvents
If StopTransfert = True Then
Close #2
Kill strNewFile
For x = 1 To 10000
DoEvents
Next x
GetFiles = False
Call ResetPB
GoTo StopGetFiles
End If
sReadBuffer = vbNullChar
bDoLoop = InternetReadFile(hFile, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
Sum = Sum + lNumberOfBytesRead
Call ProgressBar(lngFileSize, Str(Sum), strFile)
Put #2, , sBuffer
sBuffer = ""
Wend
StopGetFiles:
Close #2
InternetCloseHandle (hFile)
End Function
中的sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)问题,应为byte,但我不会改,请赐教!谢谢 --------------------编程问答-------------------- 试试
sBuffer & LeftB(sReadBuffer, lNumberOfBytesRead) --------------------编程问答-------------------- 谢谢!但还是不行!各位帮帮忙 --------------------编程问答-------------------- 不知道解决问题了吗? --------------------编程问答-------------------- 不管你解决与否,你的问题至少给了我资源的出处,我找到了答案,和你共享,也和其他人共享一下。参考了“InternetReadFile如何正确读取二进制文件 - pl2597758的专栏”
1.首先声明要修改为byte
Public Declare Function InternetReadFileByte Lib "wininet.dll" Alias "InternetReadFile" _
(ByVal hFile As Long, _
ByRef sBuffer As Byte, _
ByVal lNumberOfBytesToRead As Long, _
lNumberOfBytesRead As Long) As Integer
2.在你这段代码里,修改的如下
(1) Dim sReadBuffer(1023) As Byte
(2) bDoLoop = InternetReadFileByte(hFile, sReadBuffer(0), 1024, lNumberOfBytesRead)
'sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
'Sum = Sum + lNumberOfBytesRead
Call ProgressBar(lngFileSize, Str(Sum), strFile)
Put #2, , sReadBuffer
'sBuffer = ""
各位试试吧。 --------------------编程问答-------------------- 'Sum = Sum + lNumberOfBytesRead
改为
Sum = Sum + lNumberOfBytesRead
否则进度条无显示。
补充:VB , API