c#:检查上传的文件并继续上传

时间:2017-08-26 10:43:15

标签: c# file-upload ftp

我使用DotNetZip制作zip文件,使用this上传到ftp,两者都完美无缺。

我想检查ftp服务器上是否有丢失与服务器连接的文件。然后在再次建立连接时继续上传过程(而不是从头开始)。

以下是我的评论代码:

public void DoResume(string output,string address, string username,string password)
    {
        // create instance of uploading method
        var ftpClient = new ftp(address, username, password);
        // get the location of all files in output directory
        // output is the location of zip files
        var filesAddress = Directory.GetFiles(output);
        // get location of all files in output directory
        // var filesName = Directory.GetFiles(output).select(Path.GetDileName).ToArray();
        // get name of uploaded files
        var getuploadedfiles = ftpClient.directoryListSimple("/");
        // Upload Files
        foreach (var serverfile in getuploadedfiles)
        {
            foreach (var fadddress in filesAddress)
            {
                var outputfile = Path.GetFileName(fadddress);
                if (serverfile != outputfile)
                {
                    // save *fadddress* file as *outputfile* in server 
                    ftpClient.upload(outputfile, fadddress);
                }

            }
        }

这是代码假设要执行的操作:检查文件是否已上传,如果为true则跳转到下一个文件并再次检查,如果没有上传则执行上传此文件。

这将会发生:除了第一个文件外,它将再次上传所有文件。

1 个答案:

答案 0 :(得分:1)

此代码将检查服务器,如果之前尚未上传某些文件,则会继续上传:

public void DoResume(string output,string address, string username,string password)
    {
        var ftpClient = new ftp(address, username, password);
        // Get the name of all files in output directory
        var localfiles = Directory.GetFiles(output).Select(Path.GetFileName).ToArray();
        /* Get names of uploaded files  */
        var getuploadedfiles = ftpClient.directoryListSimple("/");
        // Get files those are not uploaded
        var diff = localfiles.Except(getuploadedfiles);
        /* Upload Files */
        foreach (var dif in diff)
        {
            foreach (var file in localfiles)
            {
                if (dif != file) continue;
                var filetoupload = $"{output}{dif}";
                ftpClient.upload(file, filetoupload);
                Console.WriteLine("{0} been uploaded", filetoupload);           
            }
        }
    }