2014-10-28 68 views
0

我正在開發一個MVC 5項目。 我需要解析大約100個文件並將解析的數據保存到數據庫中 文件來自ftp服務器。多個ftp下載

我現在做這樣的

public ActionResult ReadAll() 
    { 
     // Get all installations 
     var installations = db.Installations.ToList(); 

     // if installation unknown --> read installation 
     foreach (var installation in installations) 
     { 

      var path = string.Format(@"{0}/{1}", installation.FtpMap, "file"); 
      string fileToString = ftp.Download(path); 
      var parser = new ParseService(); 
      parser.ParseStringFile(fileToString, installation); 
      db.Entry(installation).State = EntityState.Modified; 
      db.SaveChanges(); 

     } 
     return RedirectToAction("Index"); 
    } 

public class FtpService 
{ 
    private const string Host = @"host"; 

    private readonly string user; 
    private readonly string pass; 

    private FtpWebRequest ftpRequest; 
    private FtpWebResponse ftpResponse; 


    public FtpService(string userName, string password) 
    { 
     user = userName; 
     pass = password; 
    } 

    /* Download File */ 
    public string Download(string remoteFile) 
    { 
     try 
     { 
      string ftpfullpath = string.Format("ftp://{0}/{1}.js", Host, remoteFile); 

      ftpRequest = (FtpWebRequest)WebRequest.Create(ftpfullpath); 
      ftpRequest.Credentials = new NetworkCredential(user, pass);     
      ftpRequest.UseBinary = true; 
      ftpRequest.UsePassive = true; 
      ftpRequest.KeepAlive = true; 
      ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
      ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 

      var ftpStream = ftpResponse.GetResponseStream(); 
      if (ftpStream == null) 
      { 
       throw new WebException(); 
      } 

      var streamReader = new StreamReader(ftpStream); 
      var text = streamReader.ReadToEnd(); 
      streamReader.Close(); 
      ftpStream.Close(); 
      ftpResponse.Close(); 
      ftpRequest = null; 
      return text; 
     } 
     catch (Exception ex) { Debug.WriteLine(ex.ToString()); } 
     return null; 
    } 


public class ParseService 
{ 
    private InstallationContext db = new InstallationContext(); 

    public void ParseStringFile(string fileAsText, Installation installation) 
    { 
     using (var reader = new StringReader(fileAsText)) 
     { 
      // Loop over the lines in the string. 
      string line; 
      while ((line = reader.ReadLine()) != null) 
      { 
       // do some parsing with reflection 
       Debug.WriteLine(line); 
      } 
     } 
    } 

問題是 - 它花的時間過長 - 任何人都可以給我一些指引,以提高代碼(使用任務?某種隊列? ) 在此先感謝

+0

你可以使用SignalR和啓動另一個進程,報告回Web UI,並通知作爲文件是PR ocessed http://signalr.net/ – 2014-10-28 18:47:51

+0

請記住,大多數FTP服務器限制同時連接的數量,如果你正在考慮多個線程。 – Silvermind 2014-10-28 19:05:52

回答

0

試試這個:

Parallel.Foreach (installations,()=> 
    { 

     var path = string.Format(@"{0}/{1}", installation.FtpMap, "file"); 
     string fileToString = ftp.Download(path); 
     var parser = new ParseService(); 
     parser.ParseStringFile(fileToString, installation); 
     db.Entry(installation).State = EntityState.Modified; 
     db.SaveChanges(); 

    }