473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# - Read a file from FTP and save the contents in SQL Server

47
Hi Guys,

My requirement is to scan an FTP directory for the presence of 4 files and if a specific file exists I can begin to GET those files, read the contents and save the contents into an SQL 2005 DB.

The files are of a fixed length format which map to certain elements of data that my database will exploit. If i can learn to read 1 file and put the contents into the DB then I can do the other 3 on my own.

So far I can connect to the FTP and do a GET but I do not know the solution to my problem but I do know it will be written in C# and I do not want to save the file I GET from the FTP area before I read the contents.

Please can you give me some ideas or ask me to clarify further.

See attached code i am using:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Collections.Specialized;
  4. using com.enterprisedt.net.ftp;
  5.  
  6.  
  7. namespace AA.Renewals
  8. {
  9.  
  10.     public enum FTPHelperStatus {Initialising, Ready, Busy, No_Service, No_Settings, Not_Connected, Success, Error}
  11.  
  12.     /// <summary>
  13.     /// Summary description for FTPHelper.
  14.     /// </summary>
  15.     public class FTPHelper
  16.     {
  17.  
  18.         #region "private properties"
  19.  
  20.         private string ftpTarget;
  21.         private NameValueCollection ftpSettings = new NameValueCollection();
  22.         private FTPHelperStatus status;
  23.         private FTPClient ftpClient;
  24.         private string[] _fileList;
  25.  
  26.         #endregion
  27.  
  28.         public FTPHelper(string ftpTargetKey)
  29.         {
  30.             SetFtpTarget(ftpTargetKey);  // Causes the setter to (Re)Load the settings
  31.             // on completion, status will be either "No_Settings" or "Not_Connected"
  32.         }
  33.  
  34.         private void LoadSettingsForFTPTarget()
  35.         {
  36.             status = FTPHelperStatus.Initialising;
  37.             ftpSettings.Clear();
  38.             foreach(string key in ConfigurationHelper.AppSettings.AllKeys)
  39.             {
  40.                 if (key.StartsWith(this.ftpTarget))
  41.                 {
  42.                     // just use the bits of key without the ftpTargetprefix
  43.                     string keyTail = key.Substring(ftpTarget.Length, key.Length-ftpTarget.Length);
  44.                     ftpSettings.Add(keyTail, ConfigurationHelper.AppSettings[key]);
  45.                 }
  46.             }
  47.  
  48.             status = (ftpSettings.Count > 0) ? FTPHelperStatus.Not_Connected : FTPHelperStatus.No_Settings;
  49.         }
  50.  
  51.         internal FTPClient FTPClient
  52.         {
  53.             // lazy instantiation
  54.             get
  55.             {
  56.                 if (ftpClient == null)
  57.                 {
  58.                     if (!string.IsNullOrEmpty(this.MachineName))
  59.                     {
  60.                         ftpClient = new FTPClient(this.MachineName);
  61.                     }
  62.                     else
  63.                     {
  64.                         ftpClient = new FTPClient(ConfigurationHelper.AppSettings["TestSAPFTPMachineName"]);
  65.                     }
  66.                 }
  67.                 return ftpClient;
  68.             }
  69.         }
  70.  
  71.         public bool IsReady
  72.         {
  73.             get
  74.             {
  75.                 return status == FTPHelperStatus.Ready;
  76.             }
  77.         }
  78.  
  79.         public string Setting(string settingName)
  80.         {
  81.             string retVal = ftpSettings[settingName];
  82.             return (retVal != null) ? retVal : ""; 
  83.         }
  84.  
  85.         public NameValueCollection Settings
  86.         {
  87.             get
  88.             { 
  89.                 return ftpSettings;
  90.             }
  91.         }
  92.  
  93.         public void SetFtpTarget(string ftptargetKey)
  94.         {
  95.             string ftpTargetValue = ConfigurationHelper.AppSettings[ftptargetKey];
  96.             if (ftpTargetValue != this.ftpTarget)
  97.             { 
  98.                 this.ftpTarget = ftpTargetValue; 
  99.                 // must load the new settings
  100.                 LoadSettingsForFTPTarget();                
  101.             }
  102.         }
  103.  
  104.         private void logger_OnLoggableEvent(object o, LogInfoEventArgs e)
  105.         {
  106.         //    lbProgress.Items.Add(e.Message);
  107.         //    lbProgress.SelectedIndex = lbProgress.Items.Count - 1;
  108.         //    Application.DoEvents();
  109.  
  110.         }
  111.  
  112.         private void Login()
  113.         {
  114.             FTPClient.Login(this.UserName, this.Pwd);
  115.         }
  116.  
  117.         // Added on Dec 05 2006 -- To retain the control object SocketControl
  118.         private void Login(string machineName)
  119.         {
  120.             FTPClient.Login(this.UserName, this.Pwd, machineName);     
  121.         }
  122.  
  123.         public bool CheckAvailable()
  124.         {
  125.             bool available = false;
  126.             status = FTPHelperStatus.Busy;
  127.             Login();  // this will create FTPclient if necessary, then log in
  128.             //string[] dirContents = FTPClient.Dir(".",);  // hopefully will get contents of 'root' directory
  129.             available = (FTPClient.System() != "");
  130.  
  131.            FTPClient.Quit();
  132.  
  133.             status = FTPHelperStatus.Not_Connected;
  134.  
  135.             return available;
  136.         }
  137.  
  138.         public void SendOutBoundFileStream(MemoryStream mStream, string zipFileName)
  139.         {
  140.             SendOutBoundFileStream(mStream, zipFileName, false);
  141.         }
  142.  
  143.         public void SendOutBoundFileStream(MemoryStream mStream, string zipFileName, bool test)
  144.         {
  145.             // create an instance of FTPClient, passing in the name of hte host, the time out
  146.             // and the logger for updates
  147.             if(mStream != null)
  148.             {
  149.                 mStream.Position = 0;
  150.             }
  151.             status = FTPHelperStatus.Busy;
  152.  
  153.             Login(this.MachineName);  // this will create FTPclient if necessary, then log in
  154.  
  155.             FTPClient.TransferType = FTPTransferType.BINARY;
  156.  
  157.  
  158.             // change to the output directory
  159.             try
  160.             {
  161.                 if( !test )
  162.                 {
  163.                     FTPClient.Chdir(this.OutFolder);
  164.                     status = FTPHelperStatus.Success;
  165.                 }
  166.                 else
  167.                 {
  168.                     FTPClient.Chdir(this.TestFolder);
  169.                     status = FTPHelperStatus.Success;
  170.                 }
  171.  
  172.                 try
  173.                 {
  174.  
  175.                     FTPClient.Put(mStream, zipFileName);
  176.                     status = FTPHelperStatus.Success;
  177.                 }
  178.                 catch
  179.                 {
  180.                     status = FTPHelperStatus.Error;
  181.                 }
  182.             }
  183.             catch
  184.             {
  185.                 status = FTPHelperStatus.Error;
  186.             }
  187.  
  188.             ftpClient.Quit();
  189.  
  190.  
  191.  
  192.         }
  193.  
  194.         public string[] ScanSource()
  195.         {
  196.             return ScanSource(".");
  197.         }
  198.  
  199.         public string[] ScanSource(string dir)
  200.         {
  201.             try
  202.             {
  203.                 status = FTPHelperStatus.Busy;
  204.                 Login(); // this will create FTPclient if necessary, then log in}
  205.                 if (dir != ".")
  206.                 {
  207.                     FTPClient.Chdir(dir);
  208.                 }
  209.                 _fileList = FTPClient.Dir();
  210.  
  211.             }
  212.             catch (Exception oEx)
  213.             {
  214.                 throw new Exception(string.Format("Error in AA.Renewals.FTPHelper.ScanSource(): ", oEx.Message), oEx);
  215.             }
  216.             finally
  217.             {
  218.                 FTPClient.Quit();
  219.                 status = FTPHelperStatus.Not_Connected;
  220.             }
  221.  
  222.             return _fileList;
  223.         }
  224.  
  225.         public byte[] Get()
  226.         {
  227.             byte[] ObjFtp;
  228.             try
  229.             {
  230.                 status = FTPHelperStatus.Busy;
  231.                 Login();
  232.                 ObjFtp = FTPClient.Get("test.txt");
  233.             }
  234.             catch (Exception oEx)
  235.             {
  236.                 throw new Exception(string.Format("Error in AA.Renewals.FTPHelper.Get(): {0}", oEx.Message), oEx);
  237.             }
  238.             finally
  239.             {
  240.                 ftpClient.Quit();
  241.                 status = FTPHelperStatus.Not_Connected;
  242.             }
  243.  
  244.             return ObjFtp;
  245.         }
  246.  
  247.         #region "public properties"
  248.  
  249.         public FTPHelperStatus Status
  250.         {
  251.             get { return this.status; }
  252.             set { this.status = value; }
  253.         }
  254.  
  255.         public string MachineName 
  256.         {
  257.             get
  258.             {
  259.                 string retVal = ftpSettings["MachineName"];
  260.                 return (retVal != null) ? retVal : ""; 
  261.             }
  262.         }
  263.  
  264.         public string UserName 
  265.         {
  266.             get
  267.             {
  268.                 string retVal = ftpSettings["UserName"];
  269.                 return (retVal != null) ? retVal : ""; 
  270.             }
  271.         }
  272.  
  273.         public string Pwd 
  274.         {
  275.             get
  276.             {
  277.                 string retVal = ftpSettings["Pwd"];
  278.                 return (retVal != null) ? retVal : ""; 
  279.             }
  280.         }
  281.  
  282.         public string InFolder 
  283.         {
  284.             get
  285.             {
  286.                 string retVal = ftpSettings["In"];
  287.                 return (retVal != null) ? retVal : ""; 
  288.             }
  289.         }
  290.  
  291.         public string OutFolder 
  292.         {
  293.             get
  294.             {
  295.                 string retVal = ftpSettings["Out"];
  296.                 return (retVal != null) ? retVal : ""; 
  297.             }
  298.         }
  299.  
  300.         public string TestFolder 
  301.         {
  302.             get
  303.             {
  304.                 string retVal = ftpSettings["Test"];
  305.                 return (retVal != null) ? retVal : ""; 
  306.             }
  307.         }
  308.  
  309.         #endregion
  310.  
  311.     }
  312. }
  313.  
  314.  
  315.  
Thanks in advance.
Oct 4 '08 #1
4 10979
Mr Gray
47
I can now GET the file but i have to save it to disk first - do I have to save it to disk or cannot i not do it all in memory?
Oct 5 '08 #2
Curtis Rutland
3,256 Expert 2GB
Well, I noticed in your Get() method, you are returning a Byte[].

Are you trying to save the contents of the file to the database? Or the image of the file; the binary? If you are saving the file into an Image field, you can just INSERT the byte array into the DB.

Otherwise, there is a MemoryStream object that might help. You should be able to create a StreamReader from the MemoryStream and read from the file.

Hope that helps.
Oct 6 '08 #3
Mr Gray
47
Well, I noticed in your Get() method, you are returning a Byte[].

Are you trying to save the contents of the file to the database? Or the image of the file; the binary? If you are saving the file into an Image field, you can just INSERT the byte array into the DB.

Otherwise, there is a MemoryStream object that might help. You should be able to create a StreamReader from the MemoryStream and read from the file.

Hope that helps.
Thanks for your reply.

I convert the contents into text and split on the '|' character. Each split will then map to a column in my DB. I have managed to get the contents as text and can send this to the db using SqlParameters but I would really like an example of the MemoryStream taking the byte[] and reading it as meaningful text so i can do the split without having to create a temporary file.

So if possible can you provide an example.

Many Thanks.
Oct 6 '08 #4
sashi
1,754 Expert 1GB
Well, I noticed in your Get() method, you are returning a Byte[].

Are you trying to save the contents of the file to the database? Or the image of the file; the binary? If you are saving the file into an Image field, you can just INSERT the byte array into the DB.

Otherwise, there is a MemoryStream object that might help. You should be able to create a StreamReader from the MemoryStream and read from the file.

Hope that helps.
This technique is also known as BLOB
Oct 6 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: bbxrider | last post by:
running win2k adv server/ iis5.0 trying to setup on my web page where browsers can download a file, an .xls so been trying to figure out with a download 'link' what invokes the standard ms file...
4
by: Dr. StrangeDub | last post by:
I am looking for a way to capture the HTML file generated by an ASP.Net application (just as is sent back to the client) and save it to a designated spot on the web server. Here's a bit of...
6
by: ad | last post by:
I have a compressed file in client. How could we send this compressed file (.zip) to server by web service?
2
by: Will Rickards | last post by:
In my web application there is an interactive report. Then there need to be a printable version a pdf. So I found this java tool csstoxslfo and the java fop tool from apache that will take my...
2
by: Glen Conway | last post by:
Hi, I am trying to get the contents of a file in a hidden share on a remote server, something like '\\server.domain.com\c$\program files\application\document.xml'. When I try any of the...
0
by: Binary Poet | last post by:
I am having a problem with a web service that needs to read a file on another server. Is there a way to have the web service run as another user? For example I am developing this web service on...
4
by: Ernesto | last post by:
I'm just want to read in the contents of a (text) file. The text file is filled with semiColon delimited floating point strings... 0.456;1.265;99.742;... For some reason, I can't get the...
1
by: Shashank | last post by:
Hi all, I am a new member of this community. I am making a http request to a html file placed on a Apache server. On this page there is an embeded perl statement which requires reading ...
3
by: Fons | last post by:
My script acts on a file (approx. 100k) offered by the user. Actually, it acts on a variable that should have the contents of that file in it. I could try to let users upload that file and then...
2
by: webguy262 | last post by:
I'm trying to troubleshoot a membership script. The script is attempting to write to a file called .fdaccess. The function that's throwing the error is in a file called htpasswd.php. Here's the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.