| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 695 人关注过本帖, 1 人收藏
标题:FTP传档问题
只看楼主 加入收藏
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
结帖率:97.66%
收藏(1)
 问题点数:0 回复次数:1 
FTP传档问题
FtpConnection 站上有人贴过我就不重贴了~
问问FtpConnection这段代码怎改成可传送二进位资料封包的写法?

FtpConnection原档的类模块CFtpConnection中的一小段~
程序代码:
Private Sub UploadData(lStartPoint As Long)
'--------------------------------------------------------------------------------
'Author      :Oleg Gdalevich
'Date/Time   :14.09.99
'Purpose     :Opens file, reads data from the file and
'             sends the data to remote computer by 4kb (CHANK_SIZE) chanks.
'Description :If file size is more than CHANK_SIZE the procedure called one or
'             multiple times from wscFtpData_SendComplete event procedure.
'--------------------------------------------------------------------------------
    
    Const CHANK_SIZE As Integer = 4096
    
    Static bFileIsOpen  As Boolean  'flag variable
    Static lChanksCount As Long     'quantity of chanks to send
    Static lCounter     As Long     'sent chanks counter
    Static intRemainder As Integer  '
    Dim strData         As String   'data buffer to send
    
    On Error GoTo UploadData_Err_Handler
    'if bFileIsOpen = True, the procedure was called before
    If m_bFileIsOpened Then
        'if we have to send next chank
        If lCounter < lChanksCount And lCounter > 0 Then
            'prepare the buffer
            strData = Space(CHANK_SIZE)
            'increament counter
            lCounter = lCounter + 1
            'read data from file
            Get m_intLocalFileID, , strData
            'send data
            wscData.SendData strData
        Else
            'all the data is sent
            If lCounter = 0 Then
                '
                'close data connection to inform ftp server
                'that transfer is comlteted
                '
                wscData.Close
                '
                'close local file
                '
                Close #m_intLocalFileID
                '
                RaiseEvent StateChanged(FTP_TRANSFER_COMLETED)
                '
                'reset values of all static and module
                'level variables
                '
                m_lUploadedBytes = 0:       lChanksCount = 0: intRemainder = 0
                m_bFileIsOpened = False:        m_bUploadFile = False
                '
            Else
                'all the chanks are sent
                'now we have to send the remainder
                '
                'prepare the buffer
                strData = Space(intRemainder)
                'reset the counter
                lCounter = 0
                'read data from file
                Get m_intLocalFileID, , strData
                'send data
                m_objTimeOut.StartTimer
                Do
                    DoEvents
                    '
                    If m_objTimeOut.Timeout Then
                        m_LastError = ERROR_FTP_USER_TIMEOUT
                        Exit Do
                    End If
                    '
                    If wscData.State = sckConnected Then
                        wscData.SendData strData
                        Exit Do
                    End If
                Loop
                m_objTimeOut.StopTimer
            End If
        End If
    Else
        '
        'if we are here, the procedure called at first time
        '
        m_bFileIsOpened = True  'turn on flag variable
        '
        m_intLocalFileID = FreeFile
        '
        Open m_strLocalFilePath For Binary As m_intLocalFileID
        '
        If lStartPoint > 0 Then
            Seek m_intLocalFileID, lStartPoint + 1
            m_lUploadedBytes = lStartPoint
            'get quantity of chancks to send
            lChanksCount = CLng((FileLen(m_strLocalFilePath) - lStartPoint) \ CHANK_SIZE)
            'get remainder in bytes
            intRemainder = (FileLen(m_strLocalFilePath) - lStartPoint) Mod CHANK_SIZE
        Else
            '
            'get quantity of chancks to send
            lChanksCount = CLng(FileLen(m_strLocalFilePath) \ CHANK_SIZE)
            '
            'get remainder in bytes
            intRemainder = FileLen(m_strLocalFilePath) Mod CHANK_SIZE
        End If

        If lChanksCount = 0 Then
            'if amount of data is less then 4Kb
            'prepare buffer to read data from a file
            strData = Space(intRemainder)
        Else
            '
            'prepare buffer to read data from a file
            strData = Space(CHANK_SIZE)
            'increament counter of sent chanks
            lCounter = 1
        End If
        'open file to read data
        'Open m_strLocalFilePath For Binary As #intFile
        'read data to buffer strData
        Get m_intLocalFileID, , strData
        'send data
                Do
                    DoEvents
                    If wscData.State = sckConnected Then
                        wscData.SendData strData
                        Exit Do
                    End If
                Loop
        '
        'If lCounter>0, file size if equal or less then chank size
        'and we have to send more data. At the next time this sub will
        'be called from wscData_SendComplete event procedure to send
        'next chank or remainder.
        '
    End If
    
    Exit Sub
    
Exit_Label:
    Exit Sub

UploadData_Err_Handler:
    If Not ProcessWinsockError(Err.Number, Err.Description) Then
        Err.Raise vbObjectError + 1000 + Err.Number, "CFtpConnection.UploadData", Err.Description
    End If
'    Close #intFile
    GoTo Exit_Label
End Sub


这程序有二进位传档的功能~但是只是摆好看的~实际上传二步进位档~资料都会被改写只有传的ASCII档会正常~查了好久~怀疑是这段Else
之后惹出的问题~二进位资料格式应该是字节型态吧?用String声明怪不得只会跑ASCII模式~征高手指点要怎改这后半段?

另外它有Open #没有 Close #~这也要向高手讨教一下~这也行吗?(运行上居然不会有问题)

和怎样声明 Byte * 4 的宽度?
搜索更多相关主题的帖子: 模块 资料 
2011-10-03 19:32
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
改成这样就OK了~
程序代码:
Private Sub UploadData(lStartPoint As Long)
'--------------------------------------------------------------------------------
'Author      :Oleg Gdalevich
'Date/Time   :14.09.99
'Purpose     :Opens file, reads data from the file and
'             sends the data to remote computer by 4kb (CHANK_SIZE) chanks.
'Description :If file size is more than CHANK_SIZE the procedure called one or
'             multiple times from wscFtpData_SendComplete event procedure.
'--------------------------------------------------------------------------------
    
Const CHANK_SIZE As Integer = 4096

Static bFileIsOpen  As Boolean  'flag variable
Static lChanksCount As Long     'quantity of chanks to send
Static lCounter     As Long     'sent chanks counter
Static intRemainder As Integer  '
Dim strData         As String   'data buffer to send
Dim binaryData()    As Byte     'data buffer to send
    
On Error GoTo UploadData_Err_Handler
    
    If m_TransferMode = FTP_ASCII_MODE Then
        'if bFileIsOpen = True, the procedure was called before
        If m_bFileIsOpened Then
            'if we have to send next chank
            If lCounter < lChanksCount And lCounter > 0 Then
                'prepare the buffer
                strData = Space(CHANK_SIZE)
                'increament counter
                lCounter = lCounter + 1
                'read data from file
                Get m_intLocalFileID, , strData
                'send data
                wscData.SendData strData
            Else
                'all the data is sent
                If lCounter = 0 Then
                    'close data connection to inform ftp server
                    'that transfer is comlteted
                    wscData.Close
                    'close local file
                    Close #m_intLocalFileID
                    RaiseEvent StateChanged(FTP_TRANSFER_COMLETED)
                    'reset values of all static and module
                    'level variables
                    m_lUploadedBytes = 0:       lChanksCount = 0: intRemainder = 0
                    m_bFileIsOpened = False:        m_bUploadFile = False
                Else
                    'all the chanks are sent
                    'now we have to send the remainder
                    'prepare the buffer
                    strData = Space(intRemainder)
                    'reset the counter
                    lCounter = 0
                    'read data from file
                    Get m_intLocalFileID, , strData
                    'send data
                    m_objTimeOut.StartTimer
                    Do
                        DoEvents
                        
                        If m_objTimeOut.Timeout Then
                            m_LastError = ERROR_FTP_USER_TIMEOUT
                            Exit Do
                        End If
                        
                        If wscData.State = sckConnected Then
                            wscData.SendData strData
                            Exit Do
                        End If
                    Loop
                    m_objTimeOut.StopTimer
                End If
            End If
        Else
            'if we are here, the procedure called at first time
            m_bFileIsOpened = True  'turn on flag variable
            m_intLocalFileID = FreeFile
            
            Open m_strLocalFilePath For Binary As m_intLocalFileID
            
            If lStartPoint > 0 Then
                Seek m_intLocalFileID, lStartPoint + 1
                m_lUploadedBytes = lStartPoint
                'get quantity of chancks to send
                lChanksCount = CLng((FileLen(m_strLocalFilePath) - lStartPoint) \ CHANK_SIZE)
                'get remainder in bytes
                intRemainder = (FileLen(m_strLocalFilePath) - lStartPoint) Mod CHANK_SIZE
            Else
                'get quantity of chancks to send
                lChanksCount = CLng(FileLen(m_strLocalFilePath) \ CHANK_SIZE)
                'get remainder in bytes
                intRemainder = FileLen(m_strLocalFilePath) Mod CHANK_SIZE
            End If
    
            If lChanksCount = 0 Then
                'if amount of data is less then 4Kb
                'prepare buffer to read data from a file
                strData = Space(intRemainder)
            Else
                'prepare buffer to read data from a file
                strData = Space(CHANK_SIZE)
                'increament counter of sent chanks
                lCounter = 1
            End If
            
            'open file to read data
            'Open m_strLocalFilePath For Binary As #intFile
            'read data to buffer strData
            Get m_intLocalFileID, , strData
            'send data
            
            Do
                DoEvents
                If wscData.State = sckConnected Then
                    wscData.SendData strData
                    Exit Do
                End If
            Loop
            'If lCounter>0, file size if equal or less then chank size
            'and we have to send more data. At the next time this sub will
            'be called from wscData_SendComplete event procedure to send
            'next chank or remainder.
            
        End If
        
    ElseIf m_TransferMode = FTP_IMAGE_MODE Then
    
        'if bFileIsOpen = True, the procedure was called before
        If m_bFileIsOpened Then
            'if we have to send next chank
            If lCounter < lChanksCount And lCounter > 0 Then
                'prepare the buffer
                ReDim binaryData(CHANK_SIZE)
                'increament counter
                lCounter = lCounter + 1
                'read data from file
                Get m_intLocalFileID, , binaryData
                'send data
                wscData.SendData binaryData
            Else
                'all the data is sent
                If lCounter = 0 Then
                    'close data connection to inform ftp server
                    'that transfer is comlteted
                    wscData.Close
                    'close local file
                    Close #m_intLocalFileID
                    RaiseEvent StateChanged(FTP_TRANSFER_COMLETED)
                    'reset values of all static and module
                    'level variables
                    m_lUploadedBytes = 0:       lChanksCount = 0: intRemainder = 0
                    m_bFileIsOpened = False:        m_bUploadFile = False
                Else
                    'all the chanks are sent
                    'now we have to send the remainder
                    'prepare the buffer
                    ReDim binaryData(intRemainder)
                    'reset the counter
                    lCounter = 0
                    'read data from file
                    Get m_intLocalFileID, , binaryData
                    'send data
                    m_objTimeOut.StartTimer
                    Do
                        DoEvents
                        
                        If m_objTimeOut.Timeout Then
                            m_LastError = ERROR_FTP_USER_TIMEOUT
                            Exit Do
                        End If
                        
                        If wscData.State = sckConnected Then
                            wscData.SendData binaryData
                            Exit Do
                        End If
                    Loop
                    m_objTimeOut.StopTimer
                End If
            End If
        Else
            'if we are here, the procedure called at first time
            m_bFileIsOpened = True  'turn on flag variable
            m_intLocalFileID = FreeFile
            
            Open m_strLocalFilePath For Binary As m_intLocalFileID
            
            If lStartPoint > 0 Then
            
                Seek m_intLocalFileID, lStartPoint + 1
                m_lUploadedBytes = lStartPoint
                
                'get quantity of chancks to send
                lChanksCount = CLng((FileLen(m_strLocalFilePath) - lStartPoint) \ CHANK_SIZE)
                'get remainder in bytes
                intRemainder = (FileLen(m_strLocalFilePath) - lStartPoint) Mod CHANK_SIZE
            Else
                'get quantity of chancks to send
                lChanksCount = CLng(FileLen(m_strLocalFilePath) \ CHANK_SIZE)
                'get remainder in bytes
                intRemainder = FileLen(m_strLocalFilePath) Mod CHANK_SIZE
            End If
            
            If lChanksCount = 0 Then
                'if amount of data is less then 4Kb
                'prepare buffer to read data from a file
                ReDim binaryData(intRemainder)
            Else
                'prepare buffer to read data from a file
                ReDim binaryData(CHANK_SIZE)
                'increament counter of sent chanks
                lCounter = 1
            End If
            
            'open file to read data
            'Open m_strLocalFilePath For Binary As #intFile
            'read data to buffer strData
            Get m_intLocalFileID, , binaryData
            'send data
            
            Do
                DoEvents
                If wscData.State = sckConnected Then
                    wscData.SendData binaryData
                    Exit Do
                End If
            Loop
            'If lCounter>0, file size if equal or less then chank size
            'and we have to send more data. At the next time this sub will
            'be called from wscData_SendComplete event procedure to send
            'next chank or remainder.   
        End If
    End If

Exit Sub

UploadData_Err_Handler:
    If Not ProcessWinsockError(Err.Number, Err.Description) Then
        Err.Raise vbObjectError + 1000 + Err.Number, "CFtpConnection.UploadData", Err.Description
    End If
End Sub


但是有新问题~最后一笔二进制资料不足4KB~好像会自动补0~导致传出去的档案~后面多了一堆0~
怀疑原因是VB6的Mod无法对Long型态的数字做计算~而此错误又VB6不会跳入Error Label~
因此 ReDim binaryData(intRemainder)~intRemainder值有异常封包大小没自动改变所导致~
以前有遇过相同问题~VB6中的运算元大都无法对大数字做运算~须得变个方法做出Mod的功能再取得结果~
Winsock传Ascii不会有这问题~但是传二进制会~应该是前面声明的变量型态的关系~

不要選我當版主
2011-10-04 20:04
快速回复:FTP传档问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034516 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved