|
-
Apr 20th, 2006, 08:50 PM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] BCL FTP example code help
I'm wondering if anyone knows this. I have got the FTP example application from the Base Class Libraries code samples for VB.net, which uses the class file below to download a file from the remote ftp
What is the function to send a file to the remote ftp? Does anyone know?
VB Code:
Imports System.IO
Imports System.Net
Imports System.Text
Public Class SimpleFTPClient
Public Function Download(ByVal destinationFile As String, ByVal downloadUri As Uri) As FtpStatusCode
Try
' Check if the URI is and FTP site
If Not (downloadUri.Scheme = Uri.UriSchemeFtp) Then
Throw New ArgumentException("URI is not an FTp site")
End If
' Set up the request
Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(downloadUri), FtpWebRequest)
' use the provided credentials
If Me.m_isAnonymousUser = False Then
ftpRequest.Credentials = New NetworkCredential(Me.m_userName, Me.m_password)
End If
' Download a file. Look at the other methods to see all of the potential FTP features
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile
' get the response object
Dim ftpResponse As FtpWebResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
Dim stream As Stream = Nothing
Dim reader As StreamReader = Nothing
Dim writer As StreamWriter = Nothing
' get the file as a stream from the response object and write it as
' a file stream to the local PC
Try
stream = ftpResponse.GetResponseStream
reader = New StreamReader(stream, Encoding.UTF8)
writer = New StreamWriter(destinationFile, False)
writer.Write(reader.ReadToEnd)
Return ftpResponse.StatusCode
Finally
' Allways close all streams
stream.Close()
reader.Close()
writer.Close()
End Try
Catch ex As Exception
Throw ex
End Try
End Function
Public Property UserName() As String
Get
Return Me.m_userName
End Get
Set(ByVal value As String)
Me.m_userName = value
End Set
End Property
Public Property Password() As String
Get
Return Me.m_password
End Get
Set(ByVal value As String)
Me.m_password = value
End Set
End Property
Public Property IsAnonymousUser() As Boolean
Get
Return Me.m_isAnonymousUser
End Get
Set(ByVal value As Boolean)
Me.m_isAnonymousUser = value
End Set
End Property
Private m_userName As String
Private m_password As String
Private m_isAnonymousUser As Boolean
End Class
-
Apr 21st, 2006, 08:18 PM
#2
Thread Starter
Lively Member
Re: [2005] BCL FTP example code help
I'm searching all sorts of forums and endless google search results but can't find an answer to this, perhaps there isn't one then!
-
Apr 21st, 2006, 08:33 PM
#3
Re: [2005] BCL FTP example code help
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile
The WebRequestMethods.Ftp class has a field named UploadFile too. The FtpWebRequest.GetRequestStream method is described thusly: "Retrieves the stream used to upload data to an FTP server". I'm guessing the full solution would involve those two bits of information.
-
Apr 21st, 2006, 11:23 PM
#4
Thread Starter
Lively Member
Re: [2005] BCL FTP example code help
Ah nice one jmcilhinney! I searched for WebRequestMethods.Ftp.DownloadFile and found the following code (I have changed it a bit for my needs). But it works great!
VB Code:
Public Sub Upload(ByVal source As String, ByVal target As String)
Dim credential As NetworkCredential = New NetworkCredential
credential.UserName = "helpdesk"
credential.Password = "helpdesk"
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(target), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = credential
Dim reader As New FileStream(source, FileMode.Open)
Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte
reader.Read(buffer, 0, buffer.Length)
reader.Close()
request.ContentLength = buffer.Length
Dim stream As Stream = request.GetRequestStream
stream.Write(buffer, 0, buffer.Length)
stream.Close()
Dim response As FtpWebResponse = DirectCast(request.GetResponse, FtpWebResponse)
MessageBox.Show(response.StatusDescription, "File Uploaded")
response.Close()
End Sub
-
Apr 21st, 2006, 11:39 PM
#5
Re: [2005] BCL FTP example code help
Cool. Don't forget to resolve your thread from the Thread Tools menu.
-
May 10th, 2006, 02:52 PM
#6
Re: [RESOLVED] [2005] BCL FTP example code help
I ahve similar logic as posted in the above reply but it trows an exception "The requested URI is invalid for this FTP Command."...
VB Code:
Dim request As FtpWebRequest = DirectCast(WebRequest.Create([B]target[/B]), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
'...
Dim stream As Stream = request.GetRequestStream '<<< GetRequestStream fails
I can't seemed to find any documentation on the subject.
In my case target is in the following format: ftp://ftp.server.com .
Did anybody write ftp client in vb 2005 that actually works?
Thank you guys.
-
May 10th, 2006, 05:16 PM
#7
Re: [RESOLVED] [2005] BCL FTP example code help
I just had a quick read of the documentation and it says that WeRequest.Create can handle http://, https:// and file:// by default. You need to use WebRequest.RegisterPrefix to be able to handle other protocols. RegisterPrefix requires an IWebRequestCreate object, so you'd have to define your own class that implements IWebRequestCreate. I did a quick Google for "iwebrequestcreate ftp" (without quotes) and the first page had a link to a PDF document that looked like it probably has what you're after.
http://www.google.com.au/search?clie...=Google+Search
-
May 10th, 2006, 06:09 PM
#8
Re: [RESOLVED] [2005] BCL FTP example code help
Interesting. Will take a look at that tomorrow.
Thanks man.
-
May 11th, 2006, 03:02 PM
#9
Re: [RESOLVED] [2005] BCL FTP example code help
While working on my own ftp wrapper I came across the following sample from microsoft:
How To Write Pluggable Protocol to Support FTP in Managed Classes by Using Visual Basic .NET
It's basically plug-and-play and (this part is cool) actually works.
When I'm done with my wrapper class I will post it as well.
Cheers.
-
May 11th, 2006, 03:10 PM
#10
Re: [RESOLVED] [2005] BCL FTP example code help
when i wrote an ftp application i simply used:
VB Code:
My.Computer.Network.UploadFile(SourceFile, FTPadress)
-
May 11th, 2006, 04:13 PM
#11
Re: [RESOLVED] [2005] BCL FTP example code help
Without authentication? Without creating FtpWebRequest? Without creating RequestStream? Without getting confirmation? ... etc, etc, etc ...
I wonder how that works...
-
Aug 10th, 2006, 10:26 PM
#12
New Member
Re: [RESOLVED] [2005] BCL FTP example code help
I sent some time on this. That one liner is pretty sweet but it seems to want to use passive which I don't allow on my server. So here is what I came up with. And RhinoBull your ftp address will work fine but you have to add the destination filename to it. This will validate the address for you.
Hope this saves someone some time and pain. Oh yea...this is VB 2005.
Public Function Upload(ByVal destinationFile As String, ByVal uploadUri As Uri) As FtpStatusCode
Try
'** Check if the URI is an FTP site
If Not (uploadUri.Scheme = Uri.UriSchemeFtp) Then
Throw New ArgumentException("URI is not an FTP site")
End If
'** Set up the request
Dim ftpRequest As FtpWebRequest = DirectCast(WebRequest.Create(uploadUri), FtpWebRequest)
ftpRequest.UseBinary = True
ftpRequest.Proxy = Nothing
ftpRequest.UsePassive = False
ftpRequest.Timeout = 20000
'** use the provided credentials
If Me.m_isAnonymousUser = False Then
ftpRequest.Credentials = New NetworkCredential(Me.m_userName, Me.m_password)
End If
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
Dim requestStream As Stream = ftpRequest.GetRequestStream()
'** get the file as a stream from the response object and write it as a file stream.
Dim reader As New FileStream(destinationFile, FileMode.Open)
Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte
reader.Read(buffer, 0, buffer.Length)
reader.Close()
reader = New FileStream(destinationFile, FileMode.Open)
ftpRequest.ContentLength = buffer.Length
'** Should chunk this out out in case its big.
buffer = New Byte(1024) {}
Dim bytesRead As Integer
While True
bytesRead = reader.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
requestStream.Write(buffer, 0, bytesRead)
End While
reader.Close()
'** The request stream must be closed before getting the response.
requestStream.Close()
Dim response As FtpWebResponse = DirectCast(ftpRequest.GetResponse, FtpWebResponse)
Return response.StatusCode
response.Close()
Catch ex As Exception
Globals.LogError(ex)
End Try
End Function
*** And the function this is called something like this...
Dim FTPClient As SimpleFTPClient = New SimpleFTPClient
'** This uses the Simple Client...I like this better.
FTPClient.UserName = StaticUtils.TSTDecrypt(My.Settings.FTPUser)
FTPClient.Password = StaticUtils.TSTDecrypt(My.Settings.FTPPass)
FTPClient.IsAnonymousUser = False
FTPClient.Upload(FullName, New Uri(My.Settings.FTPURL & FileName))
Later.
-
Dec 18th, 2006, 12:24 PM
#13
New Member
Re: [RESOLVED] [2005] BCL FTP example code help
jpetrie, I'm not getting this to work because my ftpRequest doesn't have members "UseBinary" nor "UsePassive". What are you using for ftpRequest?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|