# GÓC SÁNG TẠO > Khu vực lập trình > Visual Basic >  upload file bằng FTP trong VB6

## sevenup024

*Khi click vào nút Send thì đọc lấy tên file trong thư mục đã chọn rùi upload từng file lên sever.

'-----
Private Sub btnSend_Click()
On Error GoTo ErrHandle
Dim i As Integer
Dim file_name, from_dir, to_dir, host_name, username, password As String

If Trim(txtSelectFile.Text) = "" Then
MsgBox "You have not entered directory path to upload csv files !", vbSystemModal
Exit Sub
End If
If ((GetAttr(Trim(txtSelectFile.Text)) And vbDirectory) = vbDirectory) Then
from_dir = Trim(txtSelectFile.Text)
to_dir = "/var/"
host_name = "ftp://172.24.66.250/"
username = "dtngoc"
password = "dtngoc"
If Right$(from_dir, 1) <> "\" Then from_dir = from_dir & "\"
If Right$(to_dir, 1) <> "/" Then to_dir = to_dir & "/"
If Left$(to_dir, 1) <> "/" Then to_dir = "/" & to_dir
file_name = Dir$(from_dir & "*.*", vbNormal)
CommonFunctions.writeFile App.Path & "\Upload.log", "Start : " & Format(Now, "yyyy:mm:dd - hh:mm:ss")
While Len(file_name) > 0
If Not UploadFile( _
from_dir & file_name, _
to_dir & file_name, _
host_name, _
username, _
password) _
Then
If MsgBox("Continue?", _
vbYesNo Or vbQuestion, _
"Continue?") = vbNo _
Then
Exit Sub
End If
End If
file_name = Dir$(, vbNormal)
Wend
CommonFunctions.writeFile App.Path & "\Upload.log", "End : " & Format(Now, "yyyy:mm:dd - hh:mm:ss")
End If
txtResults.SetFocus
Exit Sub
ErrHandle:
Exit Sub
End Sub
'--------------
Private Function UploadFile(ByVal source_file As String, ByVal dest_file As String, ByVal host_name As String, ByVal user_name As String, ByVal passwd As String) As Boolean

On Error GoTo UploadError

Enabled = False
MousePointer = vbHourglass

If LCase$(Left$(host_name, 6)) <> "ftp://" Then host_name = "ftp://" & host_name
inetFTP.URL = host_name
inetFTP.username = user_name
inetFTP.password = passwd

If inetFTP.StillExecuting Then inetFTP.Cancel
inetFTP.Execute , "PUT " & source_file & " " & dest_file
' Wait for it to complete.
Do While inetFTP.StillExecuting
DoEvents
Loop
m_GettingDir = True
' inetFTP.Execute , "Dir"
inetFTP.Execute , "CLOSE"
CommonFunctions.writeFile App.Path & "\Upload.log", "Source File : " & source_file
CommonFunctions.writeFile App.Path & "\Upload.log", "Dest File : " & dest_file
UploadFile = True
Exit Function

UploadError:
MsgBox "Error " & Err.Number & _
" uploading file '" & _
source_file & "' to '" & _
dest_file & "'." & vbCrLf & Err.Description, _
vbExclamation Or vbOKOnly, _
"Download Error"
UploadFile = False
Exit Function
End Function
'--------------------
* Theo vninformatics.com*

----------

