Trang 1 trên 1

Upfile FTP VB6 và VB.NET

Đã gửi: T.Bảy 03/01/2015 5:24 am
gửi bởi NTN
Download và Upfile FTP VB6 :
  1. Private m_GettingDir As Boolean
  2. Private Sub AddMessage(ByVal msg As String)
  3.     txtResults.Text = txtResults.Text & vbCrLf & msg
  4.     txtResults.SelStart = Len(txtResults.Text)
  5. End Sub
  6.  
  7. Private Sub cmdDownload_Click()
  8. Dim host_name As String
  9.  
  10.     Enabled = False
  11.     MousePointer = vbHourglass
  12.     txtResults.Text = "Working"
  13.     txtResults.SelStart = Len(txtResults.Text)
  14.     DoEvents
  15.  
  16.     ' You must set the URL before the user name and
  17.    ' password. Otherwise the control cannot verify
  18.    ' the user name and password and you get the error:
  19.    '
  20.    '       Unable to connect to remote host
  21.    host_name = txtHost.Text
  22.     If LCase$(Left$(host_name, 6)) <> "ftp://" Then _
  23.         host_name = "ftp://" & host_name
  24.     inetFTP.URL = host_name
  25.  
  26.     inetFTP.UserName = txtUserName.Text
  27.     inetFTP.Password = txtPassword.Text
  28.  
  29.     ' Do not include the host name here. That will make
  30.    ' the control try to use its default user name and
  31.    ' password and you'll get the error again.
  32.    inetFTP.Execute , "Get " & _
  33.         txtRemoteFile.Text & " " & txtLocalFile.Text
  34.  
  35. '    m_GettingDir = True
  36. '    inetFTP.Execute , "Dir"
  37. End Sub
  38.  
  39. Private Sub cmdUpload_Click()
  40. Dim host_name As String
  41.  
  42.     Enabled = False
  43.     MousePointer = vbHourglass
  44.     txtResults.Text = "Working"
  45.     txtResults.SelStart = Len(txtResults.Text)
  46.     DoEvents
  47.  
  48.     ' You must set the URL before the user name and
  49.    ' password. Otherwise the control cannot verify
  50.    ' the user name and password and you get the error:
  51.    '
  52.    '       Unable to connect to remote host
  53.    host_name = txtHost.Text
  54.     If LCase$(Left$(host_name, 6)) <> "ftp://" Then _
  55.         host_name = "ftp://" & host_name
  56.     inetFTP.URL = host_name
  57.  
  58.     inetFTP.UserName = txtUserName.Text
  59.     inetFTP.Password = txtPassword.Text
  60.  
  61.     ' Do not include the host name here. That will make
  62.    ' the control try to use its default user name and
  63.    ' password and you'll get the error again.
  64.    inetFTP.Execute , "Put " & _
  65.         txtLocalFile.Text & " " & txtRemoteFile.Text
  66.  
  67. '    m_GettingDir = True
  68. '    inetFTP.Execute , "Dir"
  69. End Sub
  70.  
  71. Private Sub inetFTP_StateChanged(ByVal State As Integer)
  72.     Select Case State
  73.         Case icError
  74.             AddMessage "Error: " & _
  75.                 "    " & inetFTP.ResponseCode & vbCrLf & _
  76.                 "    " & inetFTP.ResponseInfo
  77.         Case icNone
  78.             AddMessage "None"
  79.         Case icConnecting
  80.             AddMessage "Connecting"
  81.         Case icConnected
  82.             AddMessage "Connected"
  83.         Case icDisconnecting
  84.             AddMessage "Disconnecting"
  85.         Case icDisconnected
  86.             AddMessage "Disconnected"
  87.         Case icRequestSent
  88.             AddMessage "Request Sent"
  89.         Case icRequesting
  90.             AddMessage "Requesting"
  91.         Case icReceivingResponse
  92.             AddMessage "Receiving Response"
  93.         Case icRequestSent
  94.             AddMessage "Request Sent"
  95.         Case icResponseReceived
  96.             AddMessage "Response Received"
  97.         Case icResolvingHost
  98.             AddMessage "Resolving Host"
  99.         Case icHostResolved
  100.             AddMessage "Host Resolved"
  101.  
  102.         Case icResponseCompleted
  103.             AddMessage inetFTP.ResponseInfo
  104.  
  105.             If m_GettingDir Then
  106.                 Dim txt As String
  107.                 Dim chunk As Variant
  108.  
  109.                 m_GettingDir = False
  110.  
  111.                 ' Get the first chunk.
  112.                chunk = inetFTP.GetChunk(1024, icString)
  113.                 DoEvents
  114.                 Do While Len(chunk) > 0
  115.                     txt = txt & chunk
  116.                     chunk = inetFTP.GetChunk(1024, icString)
  117.                     DoEvents
  118.                 Loop
  119.  
  120.                 AddMessage "----------"
  121.                 AddMessage txt
  122.             End If
  123.  
  124.        Case Else
  125.             AddMessage "State = " & Format$(State)
  126.     End Select
  127.  
  128.     Enabled = True
  129.     MousePointer = vbDefault
  130. End Sub
  131.  
  132.  

Hoặc tải Form Project tại đây :
howto_ftp.rar
(6.41 KiB) Đã tải 257 lần


Upfile FTP VB.NET:
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://ftp.drivehq.com/file.txt"), System.Net.FtpWebRequest)
  5.         request.Credentials = New System.Net.NetworkCredential("user", "password")
  6.         request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
  7.  
  8.         Dim file() As Byte = System.IO.File.ReadAllBytes("c:\file.txt")
  9.  
  10.         Dim strz As System.IO.Stream = request.GetRequestStream()
  11.         strz.Write(file, 0, file.Length)
  12.         strz.Close()
  13.         strz.Dispose()
  14.     End Sub
  15.  
  16. End Class


Clip tham khảo xem tại đây :


Hoặc có thể tham khảo clip này để biết cách upfile và download file FTP :


Chúc các bạn thành công ! ;)

Re: Upfile FTP VB6 và VB.NET

Đã gửi: T.Hai 05/01/2015 6:40 pm
gửi bởi tungblt