Geeks With Blogs
Michael McMorrow Space-Monkey Mafioso

I recently played with the new FTP capabilities of ASP.NET 2.0.  I created several directories on my web server, each of which harbored multiple files(text and images).  My goal was to create identically named directories on my FTP server, and then send the contents of the original web server directories to their new FTP server counterparts.  All things considered, this is a nice addition to the .NET quiver. 

Goes a little something like this:

1.) Creating  directories on an FTP server that match directories on your web server.

Dim path As String = Server.MapPath(".\directory1\")

Dim s As String

'******Iterate through the directories. 

For Each s In Directory.GetDirectories(path)

'******Some of my typically sloppy string wrangling

Dim b As Array = s.Split("\")

s = Convert.ToString(b(6))

'******Define name of target FTP server, and the name of the directory that will be created there.

Dim URI As String = "ftp://ftpservername/” + s

Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

'******Provide login info.  Security is job one.  : )

ftp.Credentials = New System.Net.NetworkCredential("username", "password")

'******Specify whether the connection should be kept alive. Default value is true.

ftp.KeepAlive = False

'******Why are we here?  In this case, to create a directory on the target FTP server.

ftp.Method = WebRequestMethods.Ftp.MakeDirectory

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)

response.Close()

End Using

Next

2.) Sending files from directories on your web server to identically named (See step 1) directories on an FTP server.

Dim d As String

'******Iterate through the directories. 

For Each d In Directory.GetDirectories(path)

'******Some of my typically sloppy string wrangling

Dim b As Array = d.Split("\")

d = Convert.ToString(b(6))

Dim f As String

'******Iterate through the files in the current directory

For Each f In Directory.GetFiles(path + d)

Dim c As Array = f.Split("\")

f = Convert.ToString(c(7))

'******Define name of target FTP server, the target FTP directory (as created in Step 1.), and the name of the file that will be sent there.

Dim URI As String = "ftp://ftpservername/ + d + “/“ + f“

'******Define the FtpWebRequest

Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

ftp.Credentials = New System.Net.NetworkCredential("username", “password")

ftp.KeepAlive = False

ftp.UseBinary = True

'******Why are we here?  In this case, to upload a file to the target FTP server.

ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile

'******Stream bytes from web server file

Dim fs As New FileStream(path + d + "/" + f, FileMode.Open)

Dim filecontents(fs.Length) As Byte

fs.Read(filecontents, 0, fs.Length)

fs.Close()

Dim requestStream As Stream = ftp.GetRequestStream()

requestStream.Write(filecontents, 0, filecontents.Length)

requestStream.Close()

Dim response As FtpWebResponse = ftp.GetResponse

response.Close()

Next

Next

 

Posted on Friday, March 10, 2006 7:56 PM | Back to top


Comments on this post: FTP and ASP.NET 2.0

# FtpWebRequest and FtpWebResponse
Requesting Gravatar...
A .NET Framework contains a rich set of classes in the System.Net namespaces that allow you to communicate...
Left by Mehrdad on Jun 02, 2006 7:29 PM

# FtpWebRequest and FtpWebResponse
Requesting Gravatar...
A .NET Framework contains a rich set of classes in the System.Net namespaces that allow you to communicate...
Left by Mehrdad Ebrahimi on Jun 02, 2006 10:56 PM

# re: FTP and ASP.NET 2.0
Requesting Gravatar...
How can I use FtpWebRequest to get a list of files in an FTP directory?
Left by Stewart on Oct 30, 2006 10:15 AM

# re: FTP and ASP.NET 2.0
Requesting Gravatar...
Hi Guys
I have a bit of problem in ASP.NET 2.0
I have developed the web page to upload files from local machine to FTP server, once I run my code locally (http://localhost:1594/Courses/admin.aspx) , it uploads the files to Ftp server, but when I published the page, the file upload didn't work. The following error is appeared:
Could not find file 'C:\aa\pages.xml'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\aa\pages.xml'.
Any one can help me????
Thanks in advanceā€¦
Left by Hanan on Mar 24, 2007 2:40 AM

# re: FTP and ASP.NET 2.0
Requesting Gravatar...
Please let me know how to upload/download multiple files using FTPWebRequest class of .Net framework - I mean how the typical mget and mput commands are wrapped up in .Net ?
Left by Jigar on Nov 29, 2007 3:39 AM

# re: FTP and ASP.NET 2.0
Requesting Gravatar...
Plz let me know anyone did download files from ftpserver
Left by Sandhya on Dec 22, 2007 10:34 PM

# re: FTP and ASP.NET 2.0
Requesting Gravatar...
I keep getting
The requested URI is invalid for this FTP command.

I dont know why
Left by chad on Feb 14, 2008 10:14 AM

# re: FTP and ASP.NET 2.0
Requesting Gravatar...
Hi All,

Can anyone tell me how to implement auto downloading file from ftp server to local pc using ftp. i tried but it is reading the file and displaying in the webpage. But i want to download the file in my pc, please tell me how to do that
here is the sample code which i tested .
--------------
protected void btnSample1_Click(object sender, EventArgs e)
{
try {
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest) WebRequest.Create("ftp://192.168.10.38/n6.txt");
request.Method = WebRequestMethods.Ftp.DownloadFile;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("", "");

FtpWebResponse response = (FtpWebResponse) request.GetResponse();

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

Response.Write(reader.ReadToEnd() + "\n\r");
//Console.WriteLine(reader.ReadToEnd());

Response.Write("Download Complete, status {0}: " + response.StatusDescription + "\n\r");
//Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

reader.Close();
response.Close();

}
catch( System.Net.WebException ex ) {
Label1.Text = "error: " + ex.Message;
}
}
--------------
Left by Girish on Mar 12, 2008 7:23 AM

# re: FTP and ASP.NET 11
Requesting Gravatar...
I want to upload file from websiteA to websiteB using ftp uri i.e., "123.45.67.8" in .NET 1.1. How can i resolve this?

Please help me in this regard.

Thanks
Left by Govind on Jan 05, 2010 8:23 AM

Your comment:
 (will show your gravatar)


Copyright © Michael McMorrow | Powered by: GeeksWithBlogs.net