首先,在VB工具欄中添加部件Microsoft Internet Transfer Control 6.0 ,之后工具箱會(huì)多一個(gè)Inet的控件。
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、新邵ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的新邵網(wǎng)站制作公司
拖放該控件到窗體上,設(shè)置控件中的屬性
protocol : 2-icFTP
RemoteHost : 你的FTP服務(wù)IP或域名
RemotePort : 你的FTP端口
URL : 你的FTPURL地址
UserName和Password 這兩個(gè)就不用說(shuō)了
'窗體中添加一個(gè)文本框(text1)和一個(gè)按鈕(Command1)代碼如下:
Private Sub Command1_Click()
Dim isRight As Boolean
Dim filestr As String
filestr = app.path "\A"
Inet1.Execute "", "PUT " filestr " /A"
isRight = Inet1.StillExecuting '獲取當(dāng)前是否仍然在執(zhí)行命令
Do While isRight
isRight = Inet1.StillExecuting
DoEvents
Loop
filestr = app.path "\B"
Inet1.Execute "", "PUT " filestr " /B"
isRight = Inet1.StillExecuting '獲取當(dāng)前是否仍然在執(zhí)行命令
Do While isRight
isRight = Inet1.StillExecuting
DoEvents
Loop
filestr = app.path "\C"
Inet1.Execute "", "PUT " filestr " /C"
isRight = Inet1.StillExecuting '獲取當(dāng)前是否仍然在執(zhí)行命令
Do While isRight
isRight = Inet1.StillExecuting
DoEvents
Loop
filestr = app.path "\D"
Inet1.Execute "", "PUT " filestr " /a/D"
isRight = Inet1.StillExecuting '獲取當(dāng)前是否仍然在執(zhí)行命令
Do While isRight
isRight = Inet1.StillExecuting
DoEvents
Loop
End Sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
text1.Text = text1.Text + CStr(State)
Select Case State
Case 11
text1.text ="在與主機(jī)通信的過(guò)程中發(fā)生了錯(cuò)誤"
Case 12
text1.Text = "上傳完成"
End Select
End Sub
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "文本文件(*.jpg)|*.jpg|所有文件(*.*)|*.*"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim FileName As String = OpenFileDialog.FileName
' TODO: 在此處添加打開(kāi)文件的代碼。
textbox1.Text = FileName
End If
第一個(gè)按鈕 上傳
Dim filelast As String = fileaddbefore.Text.Substring(fileaddbefore.Text.LastIndexOf("."), fileaddbefore.Text.Length - fileaddbefore.Text.LastIndexOf("."))
MessageBox.Show(filelast)
My.Computer.Network.UploadFile(textbox1.Text, "" 文件名.Text filelast, "登錄名1", "登錄密碼", True, 100)
第二個(gè)按鈕
net1.Protocol = icFTP
Inet1.RemoteHost = ServerName'服務(wù)器地址
Inet1.RemotePort = 21'FTP端口
Inet1.UserName = uName'用戶名
Inet1.Password = uPWS'密碼
下載文件:
Inet1.Execute "", "GET 1.txt d:\1.txt"
ri = Inet1.StillExecuting
Do While ri
ri = Inet1.StillExecuting
DoEvents
Loop
上傳文件:
Inet1.Execute "", "PUT d:\1.txt 1.txt"
ri = Inet1.StillExecuting
Do While ri
ri = Inet1.StillExecuting
DoEvents
Loop
最簡(jiǎn)單的方法是用ftp批處理,代碼如下
open app.path "\ftpcmd.ftp" for output as #1 '創(chuàng)建一個(gè)ftp命令文件
print #1, "open " "IP" '打開(kāi)ftp服務(wù)器
print #1, "user " "用戶名"
print #1, "pass " "密碼"
print #1, "lcd " "本地文件路徑" '指定本地文件所在目錄
print #1, "put " "文件名"
close #1 'frp命令文件寫(xiě)完了
shell "ftp -n -s:" app.path "\ftpcmd.ftp" '調(diào)用系統(tǒng)ftp,并執(zhí)行上面寫(xiě)的ftpcmd.ftp
My.Computer.Network.UploadFile(本地文件路徑,?ftp服務(wù)器路徑包括文件名,用戶名,密碼)