VoDA.FtpServer adalah perpustakaan server FTP sederhana. Pustaka ini menyederhanakan interaksi dengan protokol FTP hingga ke tingkat peristiwa. Semua permintaan ke server yang terkait dengan otorisasi atau bekerja dengan data menyebabkan peristiwa yang harus Anda terapkan.
Untuk memulai server, Anda perlu membuat objek FtpServerBuilder, mengkonfigurasinya menggunakan fungsi, seperti yang ditunjukkan pada contoh di bawah. Untuk informasi selengkapnya tentang setiap fungsi, lihat ConfigurationParameters. Setelah konfigurasi, panggil fungsi Build()
untuk membuat server.
Contoh server FTP untuk bekerja dengan sistem file diberikan dalam proyek Uji.
var server = new FtpServerBuilder ( )
. ListenerSettings ( ( config ) =>
{
config . Port = 21 ; // enter the port
config . ServerIp = System . Net . IPAddress . Any ;
} )
. Log ( ( config ) =>
{
config . Level = LogLevel . Information ; // enter log level. Default: Information
} )
. Certificate ( ( config ) =>
{
config . CertificatePath = ". \ server.crt" ;
config . CertificateKey = ". \ server.key" ;
} )
. Authorization ( ( config ) =>
{
config . UseAuthorization = true ; // enable or disable authorization
config . UsernameVerification += ( username ) => { .. . } ; // username verification
config . PasswordVerification += ( username , password ) => { .. . } ; //verification of username and password
} )
. FileSystem ( ( fs ) =>
{
fs . OnDeleteFile += ( client , path ) => { .. . } ; // delete file event
fs . OnRename += ( client , from , to ) => { .. . } ; // rename item event
fs . OnDownload += ( client , path ) => { .. . } ; // download file event
fs . OnGetList += ( client , path ) => { .. . } ; // get items in folder event
fs . OnExistFile += ( client , path ) => { .. . } ; // file check event
fs . OnExistFoulder += ( client , path ) => { .. . } ; // folder check event
fs . OnCreate += ( client , path ) => { .. . } ; // file creation event
fs . OnAppend += ( client , path ) => { .. . } ; // append file event
fs . OnDeleteFolder += ( client , path ) => { .. . } ; // remove folder event
fs . OnUpload += ( client , path ) => { .. . } ; // upload file event
fs . OnGetFileSize += ( client , path ) => { .. . } ; // get file size event
fs . OnGetFileModificationTime += ( client , path ) => { .. . } ; // returns the last modified date of the file
} )
. Build ( ) ;
// Start FTP-serer
server . StartAsync ( System . Threading . CancellationToken . None ) . Wait ( ) ;
Atau Anda bisa menggunakan kelas Anda sendiri yang mewarisi dari kelas konteks. Contohnya ada di bawah.
Dalam contoh ini, kelas MyAuthorization
mewarisi dari VoDA.FtpServer.Contexts.AuthorizationOptionsContext
dan kelas MyFileSystem
mewarisi dari VoDA.FtpServer.Contexts.FileSystemOptionsContext
var server = new FtpServerBuilder ( )
. ListenerSettings ( ( config ) =>
{
config . Port = 21 ; // enter the port
config . ServerIp = System . Net . IPAddress . Any ;
} )
. Log ( ( config ) =>
{
config . Level = LogLevel . Information ; // enter log level. Default: Information
} )
. Certificate ( ( config ) =>
{
config . CertificatePath = ". \ server.crt" ;
config . CertificateKey = ". \ server.key" ;
} )
. Authorization < MyAuthorization > ( )
. FileSystem < MyFileSystem > ( )
. Build ( ) ;
// Start FTP-serer
server . StartAsync ( System . Threading . CancellationToken . None ) . Wait ( ) ;
Contoh lengkap lihat di proyek Uji.