Selasa, 30 Juni 2015

Tutorial Upload File ke Linux Server Menggunakan VB .NET

- 0 komentar
Disini saya akan menggunakan aplikasi SFTP yang biasa saya gunakan untuk meremote file yaitu WinSCP.
  1.  Download WinSCP Disini
  2. Install pada komputer
  3. Remote Server menggunakan WinSCP untuk mendapatkan SSH-RSA Key guna otentikasi nantinya. Formatnya seperti di bawah inissh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
  4. Jika sudah login, masuk ke menu “Command” lalu Server/Protocol Information
    Upload File Linux Server VB .NET 1
    lalu copy Server Host Key Fingerprint untuk digunakan bagi konfigurasi di Syntax VB kita.
  5. 1.       Kita pindah ke Visual Studio, buka project lalu masuk ke Tools à Library Package Manager àPackage Manager
    Akan tampil gambar seperti di bawah ini

  6. 1.       Install kembali WinSCP dengan mengetikan “Install-Package WinSCP” (Pastikan koneksi internet berjalan baik dan tunggu hingga selesai)
    Jika telah selesai maka kita tinggal masuk ke coding yang akan digunakan
 Public Function upload_file_to_server(ByVal filename As String)
        Try
            Dim mySessionOptions As New SessionOptions
            With mySessionOptions
                .Protocol = Protocol.Sftp
                .HostName = "HOSTNAME SERVER"
                .UserName = "USER"
                .Password = "PASSWORD"
                'Isikan langkah nomor 4 kesini
                .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" 
            End With

            Using mySession As Session = New Session
                ' Connect
                mySession.Open(mySessionOptions)

                Dim stamp As String = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture)
                Dim remotePath As String = "/var/www/html/foto/"
                Dim localPath As String = filename

                ' Manual "remote to local" synchronization.

                ' You can achieve the same using:
                ' mySession.SynchronizeDirectories( _
                '     SynchronizationMode.Local, localPath, remotePath, False, False, SynchronizationCriteria.Time, _
                '     New TransferOptions With { .FileMask = fileName }).Check
                If mySession.FileExists(remotePath) Then
                    Dim upload As Boolean
                    If Not File.Exists(localPath) Then
                        Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath)
                        upload = True
                    Else
                        Dim remoteWriteTime As DateTime = mySession.GetFileInfo(remotePath).LastWriteTime
                        Dim localWriteTime As DateTime = File.GetLastWriteTime(localPath)

                        If remoteWriteTime > localWriteTime Then
                            Console.WriteLine( _
                                "File {0} as well as local backup {1} exist, " & _
                                "but remote file is newer ({2}) than local backup ({3})", _
                                remotePath, localPath, remoteWriteTime, localWriteTime)
                            upload = True
                        Else
                            Console.WriteLine( _
                                "File {0} as well as local backup {1} exist, " & _
                                "but remote file is not newer ({2}) than local backup ({3})", _
                                remotePath, localPath, remoteWriteTime, localWriteTime)
                            upload = False
                        End If
                    End If

                    If upload Then
                        Dim myTransferOptions As New TransferOptions
                        myTransferOptions.TransferMode = TransferMode.Binary
                        Dim transferResult As TransferOperationResult
                        transferResult = mySession.PutFiles(filename, remotePath, False, myTransferOptions)
                        transferResult.Check()
                        MsgBox("Upload berhasil.", MsgBoxStyle.Information, "Server Information")
                    End If
                Else
                    MsgBox("File {0} Belum Ada.", MsgBoxStyle.Exclamation, "Peringatan")
                    Console.WriteLine("File {0} Belum Ada.", remotePath)
                End If
            End Using

            Return 0
        Catch ex As Exception
            Console.WriteLine("Error: {0}", ex)
            MsgBox(ex.Message)
            Return 1
        End Try
    End Function


[Continue reading...]
 
Copyright © . Kumpulan Tutorial Sederhana - Posts · Comments
Theme Template by BTDesigner · Powered by Blogger