Solved

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

Posted on 2006-07-07
7
497 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

820 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