开发者

how get CSV FILE from the FTP server to local drive using extended stored procedures [closed]

开发者 https://www.devze.com 2023-03-14 19:59 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the开发者_高级运维 help center. Closed 11 years ago.

Please give example to get file (auto) from FTP server to local machine with the help of extended stored procedures in in c# application


If I got your point you may not inteding the SQL Stored procedures. you can from C# do the following

//Get List of Files on the FTP

    public string[] GetFileList(string URL)
            {
                string[] downloadFiles;
                StringBuilder result = new StringBuilder();
                FtpWebRequest reqFTP;
                string ftpUserID = "xyz";
                string ftpPassword = "123";
                try
                {

                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(URL));
                    reqFTP.UseBinary = true;    
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                    reqFTP.Proxy = null;
                    WebResponse response = reqFTP.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    //MessageBox.Show(reader.ReadToEnd());
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                       ProcessGotFile(line);

                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    reader.Close();
                    response.Close();
                    //MessageBox.Show(response.StatusDescription);
                    return result.ToString().Split('\n');
                }

    public void ProcessGotFile(string line)
            {
               string message_id;
               FTPclient ftp = new FTPclient();
               // download the file
               Download(@"D:\", line, "ftp://xyz:123@ftp.theFTP.com");
// Here you got `the` file locally and can start deal with it through the FileInfo class
            }

     public void Download(string filePath, string fileName, string URL)
            {
                FtpWebRequest reqFTP;
                try
                {

                    string ftpUserID = "xyz";
                    string ftpPassword = "123";
                    //filePath = <<The full path where the file is to be created.>>, 
                    //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
                    FileStream outputStream = new FileStream(filePath
    + "\\" + fileName, FileMode.Create);

                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(URL + fileName));
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    reqFTP.Timeout = 500000;
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream ftpStream = response.GetResponseStream();
                    long cl = response.ContentLength;
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[bufferSize];
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                        outputStream.Write(buffer, 0, readCount);
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }


                    ftpStream.Close();
                    outputStream.Close();
                    response.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

I hope this help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消