So I needed to write a simple function that would download a file from an Ftp location. After googling a little, I found MSN's sample code for FTP using the new FTP classes in 2.0. Here is a link to the Microsoft Download Page. After testing it out and changing the code to my needs, it worked pretty good, except that it only worked for text files. Binary files were being downloaded but the data was corrupt. I tried different things to get it to work like using a BinaryReader and Writer but I kept getting a corrupt file. Finally, I got it to work by including a single line:
ftpRequest.UseBinary = true;
So here is a basic skeleton of how to download a binary file from an FTP location:
//Create the Request Object
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("the FTP URI");
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
//Now we create a response object and ge the response stream
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Stream responseStream = ftpResponse.GetResponseStream();
//Create the destination File
FileStream DestFile = new FileStream("your file name here", FileMode.OpenOrCreate, FileAccess.ReadWrite);
//Reading the response stream and writing it to the destination file
try
{
byte[] myBuffer = new byte[2048];
int stream= 0;
do
{
stream= responseStream.Read(myBuffer , 0, myBuffer .Length);
DestFile.Write(myBuffer , 0, stream);
}
while (!(stream== 0));
}
catch (Exception ex)
{
//handle your exception here
finally
{
DestFile.Close();
}
If the method returns something, you could return the FtpCode:
return ftpResponse.StatusCode;
I Guess the file writing part could still be implemented using the BinaryReader and Writer objects, but this way worked out for me.
ASP.NET, AJAX, C#, Ruby on Rails, Web Technologies in general, and hopefully Agile Development.
Thursday, August 10, 2006
Wednesday, January 25, 2006
Ajax Revolution
I've been reading about Ajax (Asynchronous JavaScript + XML) and it seems to me like this technique is going to be the way to go for Web Application Developers. The best examples out there are Google Maps and Google Suggest. Basically Ajax lets developers create interactive web user interfaces that are comparable to desktop interfaces. Now developers can make requests to the server without having to reload the page and therefore getting rid of the clunkiness. Another advantage is that we get to to work with XML documents.
For .Net developers, more details about Ajax.Net can be found in the msdn library.
Here is a good article "Very Dynamic Web Interfaces" explaining Ajax with code samples.
A lot of big companies are switching their applications into Ajax. Even Microsoft's new version of hotmail will be heavily based on Ajax and Yahoo's new beta mail already uses Ajax. Other nice applications using this technolgy are ThinkFree and Writely.
For .Net developers, more details about Ajax.Net can be found in the msdn library.
Here is a good article "Very Dynamic Web Interfaces" explaining Ajax with code samples.
A lot of big companies are switching their applications into Ajax. Even Microsoft's new version of hotmail will be heavily based on Ajax and Yahoo's new beta mail already uses Ajax. Other nice applications using this technolgy are ThinkFree and Writely.
Monday, January 23, 2006
First Post
Following the wise advice of a good friend of mine, I started this blog to record my discoveries and adventures about the everyday new and challenging tools for Web Software Development.
I've been programming in .Net for a few months now and I am swallowing waves of information and learning as much as I can as fast as I can. I am very interested in Agile development, especially the Crystal Clear methodology and Xtreme Programming. I am learning and implementing Agile processes in my current job in the hopes that soon these techniques will start to pay off.
I've been programming in .Net for a few months now and I am swallowing waves of information and learning as much as I can as fast as I can. I am very interested in Agile development, especially the Crystal Clear methodology and Xtreme Programming. I am learning and implementing Agile processes in my current job in the hopes that soon these techniques will start to pay off.
Subscribe to:
Posts (Atom)