VB 关于FTP的传输方法问题

Mike_Squall 2017-07-28 08:06:17
是基于 autoCad的二次开发
用的是inte控件
一开始使用的是execute发送指令来传输文件
但是发现这个是多线程的方法。不容易获得我想要的传输情况的信息
意思就是说我下载完这个文件之后,立马要对文件进行操作的
但是这个方法下载的文件,还没有下载完,我就已经在对这个文件进行操作了
显然不能达到我的要求


有一个openURL的单线程方法
但是我在openURL string,1之后
inte1的参数,responseInfo返回的字符串为空
一般不应该是“该文件存在。”吗?
文件也木有正常的被读取

希望有个具体示例代码看看
...全文
257 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-08-02
  • 打赏
  • 举报
回复
仅供参考:
VERSION 5.00
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
Begin VB.Form Form1
   BorderStyle     =   1  'Fixed Single
   ClientHeight    =   3060
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4035
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3060
   ScaleWidth      =   4035
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1
      Caption         =   "下载"
      Height          =   495
      Left            =   1440
      TabIndex        =   0
      Top             =   1320
      Width           =   1215
   End
   Begin InetCtlsObjects.Inet Inet1
      Left            =   -15
      Top             =   -15
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim ExeStr As String
Dim Done As Boolean
Private Sub cmd(cmdstr As String)
    Do
        If Inet1.StillExecuting = False Then Exit Do
    Loop
    ExeStr = cmdstr
    Done = False
    Debug.Print ExeStr; "="
    Inet1.Execute , ExeStr
    Do
        DoEvents
        If Done Then Exit Do
    Loop
End Sub
Public Sub ReadNewVox(vox As String)
    Inet1.URL = "ftp://192.168.30.188"
    Inet1.UserName = "user0001"
    Inet1.Password = "password"

    'cmd "PWD"
    'cmd "CD ../../0077/125907777/vox/BBSMsg"
    'cmd "DIR"
    'cmd "GET " + vox + ".VOX " + App.Path + "\" + vox + ".VOX"

    cmd "GET ../../0077/125907777/vox/BBSMsg/" + vox + ".VOX " + App.Path + "\" + vox + ".VOX"

    cmd "QUIT"
End Sub

Private Sub Command1_Click()
    Command1.Enabled = False
    ReadNewVox ("00000021")
    Command1.Enabled = True
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim intFile As Integer
Dim vtData() As Byte
Dim str As String
'On Error GoTo ISCerr
    Debug.Print "State="; State,
    Select Case State
    Case 0
        Debug.Print "icNone"
    Case 1
        Debug.Print "icHostResolvingHost"
    Case 2
        Debug.Print "icHostResolved"
    Case 3
        Debug.Print "icConnecting"
    Case 4
        Debug.Print "icConnected"
    Case 5
        Debug.Print "icRequesting"
    Case 6
        Debug.Print "icRequestSent"
    Case 7
        Debug.Print "icReceivingResponse"
    Case 8
        Debug.Print "icResponseReceived"
        If Left(ExeStr, 2) = "CD" Then Done = True
    Case 9
        Debug.Print "icDisconnecting"
    Case 10
        Debug.Print "icDisconnected"
        Done = True
    Case 11
        Debug.Print "icError of [" + ExeStr + "]="; Inet1.ResponseInfo
        Done = True
    Case 12
        Debug.Print "icResponseCompleted----------------"
        Do
            str = Inet1.GetChunk(1024, icString)
            If LenB(str) = 0 Then Exit Do
            Debug.Print str
        Loop
        Done = True
    End Select
    Exit Sub
ISCerr:
    Resume Next
End Sub
threenewbee 2017-08-01
  • 打赏
  • 举报
回复
用inet控件,都封装好了。
of123 2017-08-01
  • 打赏
  • 举报
回复
我不管过不过时,好用我就用。 我做一个新工具两小时。他做同样功能的工具两天。他愿意进取随他。
'模板: Option Explicit Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _ (ByVal sAgent As String, ByVal LAccessType As Long, ByVal sProxyName As String, _ ByVal SProxyBypass As String, ByVal lFlags As Long) As Long Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _ (ByVal hInternetSession As Long, ByVal sServerName As String, _ ByVal nServerPort As Integer, ByVal sUsername As String, _ ByVal sPassword As String, ByVal lService As Long, _ ByVal lFlags As Long, ByVal lContext As Long) As Long Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _ (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _ ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _ ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _ ByVal dwContext As Long) As Boolean Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _ (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _ ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _ ByVal dwContext As Long) As Boolean Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" _ (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean Public Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" _ (ByVal hFtpSession As Long, ByVal lpszExsiting As String, ByVal lpszNew As String) As Boolean Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _ (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _ lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, _ ByVal dwContent As Long) As Long Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _ (ByVal hFind As Long, lpvFndData As WIN32_FIND_DATA) As Long Public Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Public Type WIN32_FIND_DATA dwFilAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName As String * 260 cAlternate As String * 14 End Type Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type '窗体: Private Sub Command1_Click() 'FTP下载 lnginet = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, _ vbNullString, vbNullString, 0&) If lnginet Then lnginetconn = InternetConnect(lnginet, "219.131.192.243", 0, _ "posui", "djposui", 1, 0, 0) If lnginetconn Then blnRC = FtpGetFile(lnginetconn, "/load.txt", "c:\load.txt", 0, 0, 1, 0) If blnRC Then MsgBox "download ok!!!" End If InternetCloseHandle lnginetconn InternetCloseHandle lnginet MsgBox "close ok!!!" Else MsgBox "can't connect" End If Else MsgBox "ftp wrong" End If End Sub Private Sub Command2_Click() 'FTP上传 lnginet = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, _ vbNullString, vbNullString, 0&) If lnginet Then lnginetconn = InternetConnect(lnginet, "219.131.192.243", 0, _ "administrator", "vai8888", 1, 0, 0) If lnginetconn Then blnRC = FtpPutFile(lnginetconn, "c:\1.txt", "/1.txt", 0, 0) If blnRC Then MsgBox "download ok!!!" End If InternetCloseHandle lnginetconn InternetCloseHandle lnginet MsgBox "close ok!!!" Else MsgBox "can't connect" End If Else MsgBox "ftp wrong" End If End Sub 方法2: '部件INET Private Sub Command1_Click() Me.Inet1.Execute Me.Inet1.URL, "send c:\11.txt /1.txt" '保存 End Sub Private Sub Command2_Click() Me.Inet1.Execute Me.Inet1.URL, "get /2.txt c:\2.txt" '下载 End Sub

2,506

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧