Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.612
  • Views Posts: 542.402
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

VB.NET - How to create directory on FTP server and Upload File or Files To FTP (Codice)

VB.NET -  How to create directory on FTP server and Upload File or Files To FTP (Codice)


 

Oggi vi voglio mettere a disposizione un programmino con il codice che Crea una cartella sul server FTP e l'altra Upload i file singolarmente o Upload di più file sul server per mezzo di una lista nel server FTP. 

CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET

Public Class Form1
    Dim ArrayMultiFile As String()
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim ServerFTP As String = "ftp://dominio.it/"
        Dim User As String = "test"
        Dim Pwd As String = "password"
        Dim FolderName As String = "httpdocs/beckup"
        txtServerFTP.Text = ServerFTP
        txtUserFTP.Text = User
        txtPasswordFTP.Text = Pwd
        txtNameFolder.Text = FolderName

        btnUploadFile.Enabled = False
        btnUploadFiles.Enabled = False
        btnCreateFolderFTP.Enabled = False

   
    End Sub
    Private Function UploadFile(ByVal UrlServerFTPByVal As String, ByVal FileUpload As String, ByVal User As String, ByVal Pwd As String) As Boolean
        Dim FileRename As String = System.IO.Path.GetFileName(FileUpload)
        If UrlServerFTPByVal.Substring(UrlServerFTPByVal.Length - 1, 1) <> "/" Then
            UrlServerFTPByVal = UrlServerFTPByVal & "/"
        End If
        Dim DestFTP As String = UrlServerFTPByVal & FileRename
        Try
            My.Computer.Network.UploadFile(FileUpload, DestFTP, User, Pwd, False, 100000)

            Return True
        Catch Errore As Exception
            '   MsgBox(Errore.Message)
            Return False
        End Try
    End Function

    Private Function CreaCartellaFTPServer(ByVal Folder_Name As String, ByVal ServerFTP As String, ByVal UserNameFTP As String, ByVal PasswordFTP As String) As Boolean
        ServerFTP = ServerFTP.Replace("ftp://", "").Replace("/", "")
        FTPSettings.IP = ServerFTP
        FTPSettings.UserID = UserNameFTP
        FTPSettings.Password = PasswordFTP
        Dim reqFTP As Net.FtpWebRequest = Nothing
        Dim ftpStream As IO.Stream = Nothing
        Try
            reqFTP = DirectCast(Net.FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + Folder_Name)), Net.FtpWebRequest)
            reqFTP.Method = Net.WebRequestMethods.Ftp.MakeDirectory
            reqFTP.UseBinary = True
            reqFTP.Credentials = New Net.NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
            Dim response As Net.FtpWebResponse = DirectCast(reqFTP.GetResponse(), Net.FtpWebResponse)
            ftpStream = response.GetResponseStream()
            ftpStream.Close()
            response.Close()
            Return True
        Catch ex As Exception
            If ftpStream IsNot Nothing Then
                ftpStream.Close()
                ftpStream.Dispose()
            End If
            If ftpStream Is Nothing Then
                'MsgBox("esiste la cartella")
                Return False
            End If
            ' Throw New Exception(ex.Message.ToString())
            Return False
        End Try
    End Function

    Public NotInheritable Class FTPSettings
        Private Sub New()
        End Sub
        Public Shared Property IP() As String
            Get
                Return m_IP
            End Get
            Set(ByVal value As String)
                m_IP = Value
            End Set
        End Property
        Private Shared m_IP As String
        Public Shared Property UserID() As String
            Get
                Return m_UserID
            End Get
            Set(ByVal value As String)
                m_UserID = Value
            End Set
        End Property
        Private Shared m_UserID As String
        Public Shared Property Password() As String
            Get
                Return m_Password
            End Get
            Set(ByVal value As String)
                m_Password = Value
            End Set
        End Property
        Private Shared m_Password As String
    End Class

    Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
        OpenFiles.InitialDirectory = "c:\"
        OpenFiles.CheckFileExists = True
        OpenFiles.CheckPathExists = True
        OpenFiles.Multiselect = False
        OpenFiles.Title = "Seleziona il Files da Upload To FTP"
        OpenFiles.Filter = "Tutti i file (*.*) |*.*"
        OpenFiles.FileName = ""
        OpenFiles.RestoreDirectory = True
        Dim filePath As String = Nothing
        If (OpenFiles.ShowDialog() = DialogResult.OK) Then
            filePath = OpenFiles.FileName.ToString()
        End If
        txtFilesUpload.Text = filePath
        btnUploadFile.Enabled = True
    End Sub

    Private Sub btnUploadFile_Click(sender As System.Object, e As System.EventArgs) Handles btnUploadFile.Click
        If (VerificaDatiServer() = True) And (txtFilesUpload.Text <> "") Then
            Dim UrlFTPFolder As String = txtServerFTP.Text & txtNameFolder.Text
            CreaFolderFTP()
            If UploadFile(UrlFTPFolder, txtFilesUpload.Text, txtUserFTP.Text, txtPasswordFTP.Text) = False Then
                MsgBox("Error File Upload To FTP Server Name File = " & IO.Path.GetFileName(txtFilesUpload.Text), vbCritical, "Errore File Upload To FTP Server")
            End If
            Application.DoEvents()
            MsgBox("Finish Upload Files To FTP Server", vbInformation, "Finish Upload To FTP")
        End If
    End Sub

   
    Private Sub btnBrowseMultiFile_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowseMultiFile.Click
        OpenFiles.InitialDirectory = "c:\"
        OpenFiles.CheckFileExists = True
        OpenFiles.CheckPathExists = True
        OpenFiles.Multiselect = True
        OpenFiles.Title = "Seleziona il Files da Upload To FTP"
        OpenFiles.Filter = "Tutti i file (*.*) |*.*"
        OpenFiles.FileName = ""
        OpenFiles.RestoreDirectory = True
        Dim filePath As String = Nothing
        ArrayMultiFile = Nothing
        If (OpenFiles.ShowDialog() = DialogResult.OK) Then
            ArrayMultiFile = OpenFiles.FileNames

            If lstMultiFiles.Items.Count >= 0 Then
                For i As Integer = 0 To ArrayMultiFile.Length - 1
                    lstMultiFiles.Items.Add(ArrayMultiFile(i).ToString)
                Next
                btnUploadFiles.Enabled = True
            End If

        End If
    End Sub

    Private Sub btnUploadFiles_Click(sender As System.Object, e As System.EventArgs) Handles btnUploadFiles.Click
        If (VerificaDatiServer() = True) And (lstMultiFiles.Items.Count >= 0) Then
            lstMultiFiles.Enabled = False
            Application.DoEvents()
            CreaFolderFTP()
            Dim UrlFTPFolder As String = txtServerFTP.Text & txtNameFolder.Text
            For i As Integer = 0 To lstMultiFiles.Items.Count - 1
                If UploadFile(UrlFTPFolder, lstMultiFiles.Items(i).ToString, txtUserFTP.Text, txtPasswordFTP.Text) = False Then
                    MsgBox("Error File Upload To FTP Server Name File = " & IO.Path.GetFileName(lstMultiFiles.Items(i).ToString), vbCritical, "Errore File Upload To FTP Server")
                End If
                Application.DoEvents()
            Next i
            MsgBox("Finish Upload Files To FTP Server", vbInformation, "Finish Upload To FTP")
            lstMultiFiles.Enabled = True
        End If
    End Sub

    Private Sub btnCreateFolderFTP_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateFolderFTP.Click
        If (VerificaDatiServer() = True) And (txtNameFolder.Text <> "") Then
            If CreaCartellaFTPServer(txtNameFolder.Text, txtServerFTP.Text, txtUserFTP.Text, txtPasswordFTP.Text) = True Then
                MsgBox("Created folder on the FTP Server ", vbInformation, "Created folder on the FTP Server")
            Else
                MsgBox("Error existing folder or Server Error unresponsive", vbCritical, "Error existing folder or Server Error unresponsive")
            End If
        End If
    End Sub

    Private Function VerificaDatiServer() As Boolean
        If (txtServerFTP.Text <> "") And (txtUserFTP.Text <> "") And (txtPasswordFTP.Text <> "") Then
            Return True
        End If
        Return False
    End Function


    Private Sub ValidateValore()
        txtNameFolder.Text = txtNameFolder.Text.Trim()
        If txtNameFolder.Text <> Nothing Or txtNameFolder.Text <> "" Then
            btnCreateFolderFTP.Enabled = True
        Else
            btnCreateFolderFTP.Enabled = False
        End If
    End Sub
    Private Sub txtNameFolder_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtNameFolder.TextChanged
        ValidateValore()
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        End
    End Sub

    Private Sub CreaFolderFTP()
        Dim folder_name As String = Nothing
        If (VerificaDatiServer() = True) Then
            If (txtNameFolder.Text = "") Then
                folder_name = "FolderName" & "_" & Date.Now.Day & "_" & Date.Now.Month & "_" & Date.Now.Year
            Else
                folder_name = txtNameFolder.Text
            End If
            CreaCartellaFTPServer(folder_name, txtServerFTP.Text, txtUserFTP.Text, txtPasswordFTP.Text)
        End If
    End Sub

 
    Private Sub btnClearList_Click(sender As System.Object, e As System.EventArgs) Handles btnClearList.Click
        lstMultiFiles.Items.Clear()
    End Sub
End Class


  

Per i pigri o sfaticati potete scaricare il file d'esempio :

Password :  "ImaginSystem" 
Link File Download : Download Upload File or Files FTP 

By ImaginSystems & Queen Gin
Categoria: VB.NET
lunedì, 25 feb 2013 Ore. 19.53

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003