Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多ftp下载_C#_Asp.net Mvc_Ftp - Fatal编程技术网

C# 多ftp下载

C# 多ftp下载,c#,asp.net-mvc,ftp,C#,Asp.net Mvc,Ftp,我正在做一个MVC5项目。 我需要解析大约100个文件,并将解析后的数据保存到数据库中 文件来自ftp服务器 我现在是这样做的 public ActionResult ReadAll() { // Get all installations var installations = db.Installations.ToList(); // if installation unknown --> read installation

我正在做一个MVC5项目。 我需要解析大约100个文件,并将解析后的数据保存到数据库中 文件来自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);  
            }
        }
    }
问题是——它需要很长时间-- 有人能给我一些改进代码的指导方针吗(处理任务?某种队列?) 提前感谢

试试这个:

 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();

    }

您可以使用Signal并启动另一个进程,该进程将报告回Web UI,并在处理文件时通知。请记住,如果您考虑多个线程,大多数ftp服务器都会限制同时连接的数量。