当前位置: 源码素材网 » VB教程 » 详情页

Vb利用FTP上传文件到远程服务器

  •   时间:2018-12-15
  • 概述:FTP 上传文件

Vb上传文件例子,使用ftp的方法来上传文件,再此提示,请确保上传的目标服务器网络状态正常,否则程序可能运行中假死,本代码要借助于MSINET.OCX、MSCOMCTL.OCX等控件来实现:

VERSION 5.00
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1
   Caption         =   "上传文件"
   ClientHeight    =   5355
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   8760
   LinkTopic       =   "Form1"
   ScaleHeight     =   5355
   ScaleWidth      =   8760
   StartUpPosition =   2  '屏幕中心
   Begin VB.CommandButton cmdSend
      Caption         =   "上载"
      Height          =   405
      Left            =   3045
      TabIndex        =   7
      Top             =   4800
      Width           =   1275
   End
   Begin VB.Frame Frame2
      Caption         =   "服务器文件"
      Height          =   4575
      Left            =   4425
      TabIndex        =   2
      Top             =   120
      Width           =   4260
      Begin VB.TextBox txtURL
         Height          =   375
         Left            =   1215
         TabIndex        =   8
         Text            =   "ftp://192.168.1.125"
         Top             =   210
         Width           =   2940
      End
      Begin MSComctlLib.ListView lv
         Height          =   3735
         Left            =   150
         TabIndex        =   3
         Top             =   705
         Width           =   4005
         _ExtentX        =   7064
         _ExtentY        =   6588
         View            =   3
         LabelWrap       =   -1  'True
         HideSelection   =   0   'False
         AllowReorder    =   -1  'True
         HoverSelection  =   -1  'True
         _Version        =   393217
         Icons           =   "ImageList1"
         SmallIcons      =   "ImageList1"
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BorderStyle     =   1
         Appearance      =   1
         NumItems        =   0
      End
      Begin VB.Label Label1
         Caption         =   "服务器地址:"
         Height          =   225
         Index           =   0
         Left            =   135
         TabIndex        =   9
         Top             =   315
         Width           =   1140
      End
   End
   Begin VB.Frame Frame1
      Caption         =   "源文件"
      Height          =   4575
      Left            =   90
      TabIndex        =   1
      Top             =   120
      Width           =   4260
      Begin VB.DirListBox Dir1
         Height          =   1350
         Left            =   105
         TabIndex        =   6
         Top             =   645
         Width           =   3915
      End
      Begin VB.DriveListBox Drive1
         Height          =   300
         Left            =   135
         TabIndex        =   5
         Top             =   285
         Width           =   3915
      End
      Begin VB.FileListBox File1
         Height          =   2430
         Left            =   105
         TabIndex        =   4
         Top             =   2025
         Width           =   3915
      End
   End
   Begin MSComctlLib.ImageList ImageList1
      Left            =   8010
      Top             =   -180
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      ImageWidth      =   32
      ImageHeight     =   32
      MaskColor       =   12632256
      _Version        =   393216
      BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628}
         NumListImages   =   2
         BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628}
            Picture         =   "Form1.frx":0000
            Key             =   ""
         EndProperty
         BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628}
            Picture         =   "Form1.frx":08DA
            Key             =   ""
         EndProperty
      EndProperty
   End
   Begin VB.CommandButton cmdDIR
      Caption         =   "浏览"
      Height          =   405
      Left            =   4440
      TabIndex        =   0
      Top             =   4800
      Width           =   1275
   End
   Begin InetCtlsObjects.Inet Inet1
      Left            =   7230
      Top             =   -90
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
   End
   Begin VB.Label Label2
      Height          =   495
      Left            =   105
      TabIndex        =   10
      Top             =   4755
      Width           =   2775
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public StateStyle As Integer
Private Sub cmdDIR_Click()
  StateStyle = 0
  Inet1.AccessType = icUseDefault  '设置与Internet连接的类型,默认值
  Inet1.Protocol = icFTP
  Inet1.RemotePort = 21
  bln = False
  Inet1.Execute txtURL.Text, "DIR "   '检索目录
End Sub
Private Sub cmdSend_Click()
   Dim myfilepath As String
   myfilepath = File1.Path & "\" & File1.FileName
   StateStyle = 1
   Inet1.Execute txtURL.Text, "SEND " & myfilepath & " " & File1.FileName     '上载文件
End Sub
Private Sub Dir1_Change()
  File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
  Dir1.Path = Drive1.Drive
End Sub
Private Sub Form_Load()
  StateStyle = 0
  lv.ColumnHeaders.Add , , "文件", lv.Width / 1.5
  Inet1.Execute txtURL.Text, "DIR "   '检索目录
End Sub
Private Sub Inet1_StateChanged(ByVal state As Integer)
   'State = 12 时,用 GetChunk 方法检索服务器的响应。
   Dim vtData As Variant
   Dim inetdata
   Dim i As Integer
   Select Case state
   Case icHostResolvingHost
      Label2.Caption = "正在查询所指定的主机的 IP 地址"
   Case icHostResolved
      Label2.Caption = "已成功地找到所指定的主机的 IP 地址"
   Case icConnecting
      Label2.Caption = "正在与主机连接"
   '...没有列举其他情况。
   Case icResponseReceived
      Label2.Caption = "已成功地接收到主机的响应"
   Case icError '11
      '出现错误时,返回 ResponseCode 和 ResponseInfo。
      vtData = Inet1.ResponseCode & ":" & _
      Inet1.ResponseInfo
   Case icResponseCompleted    '12
     Select Case StateStyle
      Case 0
       lv.ListItems.Clear
       inetdata = Inet1.GetChunk(1024, 0)
       If Trim(inetdata) <> 0 Then
         vtData = Split(inetdata, vbCrLf, , vbTextCompare)
         i = 0
         Do While i < UBound(vtData)
           If vtData(i) <> "" Then
             If Right(Trim(vtData(i)), 1) <> "/" Then
               lv.ListItems.Add , , vtData(i), 2, 2
             Else
                lv.ListItems.Add , , Mid(vtData(i), 1, Len(vtData(i)) - 1), 1, 1
             End If
           End If
           i = i + 1
         Loop
       End If
      Case 1
       inetdata = Inet1.GetChunk(1024, 0)
       If Trim(inetdata) <> 0 Then
       vtData = Split(inetdata, vbCrLf, , vbTextCompare)
       i = 0
       Do While i < UBound(vtData)
         If vtData(i) <> "" Then
           If Right(Trim(vtData(i)), 1) <> "/" Then
             lv.ListItems.Add , , vtData(i), 2, 2
           Else
              lv.ListItems.Add , , Mid(vtData(i), 1, Len(vtData(i)) - 1), 1, 1
           End If
         End If
         i = i + 1
       Loop
       End If
     End Select

   End Select
End Sub
Private Sub lv_DblClick()
  Inet1.Execute txtURL.Text, "DIR " & "/" & lv.SelectedItem & "/"    '检索目录
End Sub

    相关内容:

    相关声明:

      若“Vb利用FTP上传文件到远程服务器”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。