Mar 18, 2008

Ftp Client

Hehheheheh, setelah beberapa lama, mendem perasaan akhirnya gak kuasa juga untuk tida sharring dengan teman-teman semua. Aku ada class untuk FTP client yang saya buat dengan menggunakan Visual Basic 8 untuk .Net Framework 2.0. Intinya class tersebut saya buat untuk memudahkan saya dalam menggunakan protocl FTP. tapi sayangnya saya gak punya server yang bisa dijadikan tempat menyimpan file tersebut, jadi sementara bagi yang berminat langsung kirim email ke wawan_redhat@yahoo.com saja. Ok.
Nich saya kasih satu fungsi

---------------------------------begin code---------------------------------
'*********************************************
' Upload : Upload file to remote ftp server
'*********************************************
Public Function Upload(ByVal FileName As String, ByVal FtpStringRequestPath As String) As Boolean
'Declare variable to store file name and path to the ftp server
Dim FileNameFtpStringRequest As String = String.Empty

'Set the FileNameFtpStringRequest
Try
FileNameFtpStringRequest = FtpStringRequestPath + "/" + Path.GetFileName(FileName)
Catch ex As Exception
_exception = New Exception(ex.Message)
Return False
End Try

'Try to connecting this app to remote server
Try
'Set the url as web request
_uri = New Uri(FileNameFtpStringRequest)

_ftpWebRequest = WebRequest.Create(FileNameFtpStringRequest)
'log-in to server
_ftpWebRequest.Credentials = New NetworkCredential(_username, _password)
_ftpWebRequest.KeepAlive = True
Catch ex As UriFormatException
_exception = New Exception(ex.Message)
Return False
End Try
'send ftp command
Try
'Set the ftp command to delete file
_ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
'Send request to ftp server
_ftpStream = _ftpWebRequest.GetRequestStream()

'Check the file will be uploaded is present
If Not File.Exists(FileName) Then
_exception = New Exception(ERR_FILE_NOT_EXIST)
'Close connection to ftp server
_ftpWebResponse.Close()
'Close and delete _fileStream so another function can use the variable
_ftpStream.Close()
_ftpStream.Dispose()

Return False
End If

'read all content of file and send by stream
_fileStream = File.Open(FileName, FileMode.Open) 'Open file

Dim buffer(1024) As Byte
Dim bytesRead As Integer
'send file content by 1024 bytes
While True
bytesRead = _fileStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
_ftpStream.Write(buffer, 0, bytesRead)
End While

'Close connection to ftp server
_ftpWebResponse.Close()
'Close and delete _fileStream so another function can use the variable
_ftpStream.Close()
_ftpStream.Dispose()
'Close and delete _fileStream so another function can use the variable
_fileStream.Close()
_fileStream.Dispose()

Return True
Catch ex As WebException
_exception = New Exception(ex.Message)
Return False
End Try
End Function

---------------------------------end code-----------------------------------
Powered by Blogger.

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search