Announcement

Collapse
No announcement yet.

FTP-Zugriff für up- und download mit VB.NET

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • FTP-Zugriff für up- und download mit VB.NET

    Hallo,

    kann mir jemand sagen, wie ich einen UP-, bzw. Download über das FTP-Protokol mit Visual Basic .NET realisieren kann?

    Die VS.NET Hilfe zum "Programmieren von austauschbaren Protokollen' hilft mir nicht weiter:

    WebRequest.RegisterPrefix("ftp", New FtpWebRequestCreator())
    Dim req As WebRequest = WebRequest.Create("ftp://ftp.contoso.com/")

    Hat jemand eine Idee?

    Danke.

  • #2
    ' Download a remote file to a local file name and include a path. Then, set the
    ' resume flag. The local file name will be created or will be overwritten, but the path must exist.
    Public Sub DownloadFile(ByVal sFileName As String, _
    ByVal sLocalFileName As String, _
    ByVal bResume As Boolean)
    Dim st As Stream
    Dim output As FileStream
    Dim cSocket As Socket
    Dim offset, npos As Long

    SetBinaryMode(True)

    If (sLocalFileName.Equals("")) Then
    sLocalFileName = sFileName
    End If

    If (Not (File.Exists(sLocalFileName))) Then
    st = File.Create(sLocalFileName)
    st.Close()
    End If

    output = New FileStream(sLocalFileName, FileMode.Open)
    cSocket = CreateDataSocket()
    offset = 0

    If (bResume) Then
    offset = output.Length

    If (offset > 0) Then
    'Send an FTP command to restart.
    SendCommand("REST " & offset)
    If (m_iRetValue <> 350) Then
    offset = 0
    End If
    End If

    If (offset > 0) Then
    npos = output.Seek(offset, SeekOrigin.Begin)
    End If
    End If
    'Send an FTP command to retrieve a file.
    SendCommand("RETR " & sFileName)

    If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
    MessageString = m_sReply
    Throw New IOException(m_sReply.Substring(4))
    End If

    Do While (True)
    m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
    m_iBytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
    output.Write(m_aBuffer, 0, m_iBytes)

    If (m_iBytes <= 0) Then
    Exit Do
    End If
    Loop

    output.Close()
    If (cSocket.Connected) Then
    cSocket.Close()
    End If

    ReadReply()
    If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then
    MessageString = m_sReply
    Throw New IOException(m_sReply.Substring(4))
    End If

    End Sub

    bzw.

    ' This is a function that is used to upload a file from your local hard disk to your FTP site
    ' and then set the resume flag.
    Public Sub UploadFile(ByVal sFileName As String, _
    ByVal bResume As Boolean)
    Dim cSocket As Socket
    Dim offset As Long
    Dim input As FileStream
    Dim bFileNotFound As Boolean

    cSocket = CreateDataSocket()
    offset = 0

    If (bResume) Then
    Try
    SetBinaryMode(True)
    offset = GetFileSize(sFileName)
    Catch ex As Exception
    offset = 0
    End Try
    End If

    If (offset > 0) Then
    SendCommand("REST " & offset)
    If (m_iRetValue <> 350) Then

    'The remote server may not support resuming.
    offset = 0
    End If
    End If
    'Send an FTP command to store a file.
    SendCommand("STOR " & Path.GetFileName(sFileName))
    If (Not (m_iRetValue = 125 Or m_iRetValue = 150)) Then
    MessageString = m_sReply
    Throw New IOException(m_sReply.Substring(4))
    End If

    'Check to see if the file exists before the upload.
    bFileNotFound = False
    If (File.Exists(sFileName)) Then
    ' Open the input stream to read the source file.
    input = New FileStream(sFileName, FileMode.Open)
    If (offset <> 0) Then
    input.Seek(offset, SeekOrigin.Begin)
    End If

    'Upload the file.
    m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)
    Do While (m_iBytes > 0)
    cSocket.Send(m_aBuffer, m_iBytes, 0)
    m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)
    Loop
    input.Close()
    Else
    bFileNotFound = True
    End If

    If (cSocket.Connected) Then
    cSocket.Close()
    End If

    'Check the return value if the file was not found.
    If (bFileNotFound) Then
    MessageString = m_sReply
    Throw New IOException("The file: " & sFileName & " was not found." & _
    " Cannot upload the file to the FTP site.")

    End If

    ReadReply()
    If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then
    MessageString = m_sReply
    Throw New IOException(m_sReply.Substring(4))
    End If
    End Su

    Comment


    • #3
      Vielen Dank

      Comment

      Working...
      X