用FTP下载EXE程序、word文档,文件变大,打不开
我在局域网内用SII建立了一个FTP空间。放文本文档,word文档,EXE程序,传输完成后,只有文本文档正常。其它文件都会变大了。代码是网上找的如下:请各位帮忙看看。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 * 4096
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)
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
vMode As Integer 传 binary 了么 to caozhy :
两种情况都试过,结果一样。麻烦再帮我研究下,谢谢!
If vMode = 0 Then '
Transfer = FTP_TRANSFER_TYPE_ASCII
Else
Transfer = FTP_TRANSFER_TYPE_BINARY
End If
用ultraedit或者某个十六进制编辑器比较正确和错误的文件,差别在哪里。 to caozhy :
有成熟的代码吗?
各位高手,提供一些成熟的代码啊!谢谢。
我的邮箱:122886464@qq.com 这3行:
sReadBuffer = vbNullChar
bDoLoop = InternetReadFile(hFile, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
sReadBuffer先赋值为NULL,然后读取“ Len(sReadBuffer) ”,岂不就是读一个字符!
然后: sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
纯粹多此一举,直接 sBuffer = Left$(sReadBuffer, lNumberOfBytesRead) 就行了。
既然会下载 非文本格式 的文件,楼主干吗不直接用字节数组作为缓冲区来读取?
一次请求几K,甚至几十K的内容,应该更高效些吧!
还有,用字节数组来读取,我觉得这样应该不会出问题。
晕,刚注意到 sReadBuffer 是定长字符串。
那 sReadBuffer = vbNullChar 这句就是多余的了。
楼主可以试下这样改:
先把 sReadBuffer 的定义改成:
Dim sReadBuffer(1 To 4096) As Byte
然后,后面读取及写入的地方:
'sReadBuffer = vbNullChar谢谢 Chen8013 的帮助
bDoLoop = InternetReadFile(hFile, sReadBuffer, 4096&, lNumberOfBytesRead)
'sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
'If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
If (lNumberOfBytesRead = 0) Then
bDoLoop = False
ElseIf (lNumberOfBytesRead < 4096) Then
bDoLoop = False
ReDim Preserve sReadBuffer(lNumberOfBytesRead)
End If
Sum = Sum + lNumberOfBytesRead
Call ProgressBar(lngFileSize, Str(Sum), strFile) '状态条显示
'Put #2, , sBuffer
If (lNumberOfBytesRead) Then Put #2, , sBuffer
'sBuffer = ""
我试试
补充:VB , 网络编程