Zurück   Entwickler-Forum > .NET > VB.NET

Antwort
 
Themen-Optionen Ansicht

FTP-Zugriff für up- und download mit VB.NET
Alt 29.03.2005, 19:40   #1
peter.schuster
Neuer Benutzer
 
peter.schuster ist offline
Registriert seit: 29.03.2005
Beiträge: 3
peter.schuster befindet sich auf einem aufstrebenden Ast
Standard 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.
  Mit Zitat antworten

Anzeige

Alt 13.04.2005, 15:32   #2
Kathrin Haake
Neuer Benutzer
 
Kathrin Haake ist offline
Registriert seit: 01.03.2005
Beiträge: 7
Kathrin Haake befindet sich auf einem aufstrebenden Ast
Standard

' 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
  Mit Zitat antworten

Alt 02.05.2005, 13:45   #3
peter.schuster
Neuer Benutzer
 
peter.schuster ist offline
Registriert seit: 29.03.2005
Beiträge: 3
peter.schuster befindet sich auf einem aufstrebenden Ast
Standard

Vielen Dank
  Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu



Alle Zeitangaben in WEZ +2. Es ist jetzt 03:50 Uhr.



© 1995–2009 Software & Support Verlag GmbH. Vervielfältigung nur mit Genehmigung des Verlags.