![]() |
#2
邵帅2012-10-13 09:22
|

private Declare Sub InternetWriteFile Lib "wininet.dll" (ByRef hFile As Long, lpBuffer As Byte, ByVal dwNumberOfBytesToWrite As Long, ByRef lpdwNumberOfBytesWritten As Long)
Private Const BUFFERSIZE As Long = 255 '缓冲区大小
Public Function ftpuploadfile(slocal As String, sremote As String) As Boolean '''上传文件
Dim hFile As Long
Dim data(BUFFERSIZE - 1) As Byte '存放数据的缓冲区
Dim written As Long ''已写
Dim size As Long '总大小
Dim sum As Long '已经下载的大小
Dim lblock As Long '块数
ftpuploadfile = False
sum = 0
lblock = 0
hFile = FtpOpenFile(hconnection, sremote, GENERIC_WRITE, dwtype, 0) '打开文件
If hFile = 0 Then
Errorout Err.LastDllError, "打开文件"
Exit Function
End If
Open slocal For Binary Access Read As #1 '打开本地文件
size = LOF(1)
For lblock = 1 To size \ BUFFERSIZE '分块读写
Get #1, , data
InternetWriteFile hFile, data(0), BUFFERSIZE, written
' If InternetWriteFile(hFile, data(0), BUFFERSIZE, written) = 0 Then '写
' Errorout Err.LastDllError, "写文件"
' MsgBox ("123445")
' Close #1
' Exit Function
' End If
DoEvents '''''''''''''''交出控制权
sum = sum + BUFFERSIZE
RaiseEvent filetransferprogress(sum, size) '触发filetransferprogress
Next lblock
ReDim Data2((size Mod BUFFERSIZE) - 1) As Byte '读写剩余部分
Get #1, , Data2
InternetWriteFile hFile, Data2(0), size Mod BUFFERSIZE, written
' If InternetWriteFile(hFile, Data2(0), size Mod BUFFERSIZE, written) = 0 Then '读
' Errorout Err.LastDllError, "读文件剩余部分"
' Close #1
' Exit Function
' End If
sum = sum + size Mod BUFFERSIZE
RaiseEvent filetransferprogress(sum, size) '触发filetransferprogress
'MsgBox ("正在读写剩余部分" + "sum=" + CStr(sum) + "" + "size=" + CStr(size))
Close #1
InternetCloseHandle (hFile)
ftpuploadfile = True
End Function
[ 本帖最后由 邵帅 于 2012-10-13 09:12 编辑 ]