Solved

ASP.NET webservice - Connect to FTP Web server - Read Text file.

Posted on 2006-07-07
7
484 Views
Last Modified: 2011-09-20
EE Team,

I have a project design, wherein i need to develop a asp.net web service application, this application will constantly look in to a FTP Server.

So other application will place a text file on to the Secure FTP Server, and my webservice will look for this text file, if the text file is found, it will read this file and update some table.

The problem here is what kind of programming method should i use, please give me some examples..from web....

1. Web service to connect to FTP Server.
2. How the web service will know, if there is any text file in the folder.
3. How to read the text file.

Please let me know you thought or sample example how can i acheive this design.

Thanks
0
Comment
Question by:Apu_Shah
  • 3
7 Comments
 
LVL 3

Expert Comment

by:mdg12
ID: 17066998
There are several examples on the web...  Take for example the API at
http://www.csharphelp.com/archives/archive9.html

Copy the first class into FTPFactory.cs
Then in your webservice method, do something like the following
   FTPFactory ff = new FTPFactory();
   ff.setRemoteHost("192.168.0.30");
   ff.setRemoteUser("username");
   ff.setRemotePass("password");
   ff.login();
   ff.chdir("/dir");
   
   // Check if file exists
   String[] fileNames = ff.getFileList("filename.txt");
   if (fileNames==null || fileNames.Length!=1) {
     // File not found
     return;
   }
   
   // File is there, download it
   ff.setBinaryMode(true);
   ff.download("filename.txt");
   
   // Remove it??
   ff.deleteRemoteFile("filename.txt");
   
   ff.close();
   
   DoSomethingWithFile();
0
 
LVL 2

Author Comment

by:Apu_Shah
ID: 17068876
I looked at this code, i think it will work as expected.

I had one more question : The design says " Webservice will always loop and look for a one text file. if the file is found continue read the file and process " I am not sure how will this code make sure to loop and look for the file and once the file is arrived at the ftp server - folder location. process it.

I am going to try this, also i need help or sample code - to read a tab delimited text files.

Thanks.
0
 
LVL 3

Accepted Solution

by:
mdg12 earned 500 total points
ID: 17070302
I'm not positive I understand the design (and certainly not the rationale), but here's an example of how you might loop until you find the file...

Also at the end is a sample of how you might load the file to begin processing it.  I'm not sure what sort of processing you'll need to do, but below shows you how to read it line by line.. After that, you'll probably want to use either string functions or regular expressions

   FTPFactory ff = new FTPFactory();
   ff.setRemoteHost("192.168.0.30");
   ff.setRemoteUser("username");
   ff.setRemotePass("password");
   ff.login();
   ff.chdir("/dir");
   
   while (true)
   {
      // Check if file exists
      string[] fileNames = ff.getFileList("filename.txt");
      if (fileNames==null || fileNames.Length!=1) {
        // File not found, loop again
        continue;
      }
        // File exists, proceed
        break;
   }
   
   // File is there, download it
   ff.setBinaryMode(true);
   ff.download("filename.txt");
   
   // Remove it from remote server??
   ff.deleteRemoteFile("filename.txt");
   
   ff.close();
   
   // Load the local file
   StreamReader re = File.OpenText("filename.txt");
   string line = null;
   while ((line = re.ReadLine()) != null) {
      //TODO: Process line
   }
   re.close;




References:
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=132
http://www.csharphelp.com/archives/archive9.html
0
 
LVL 3

Expert Comment

by:mdg12
ID: 17471310
pinaldave - I don't mean to be a pointwhore, but I think my responses answered his question...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now