예제 #1
0
        // Extract data from the directory
        static void ExtractImageData()
        {
            Student student = new Student()
            {
                AbsoluteUrl = Constants.FTP.BaseUrl
            };
            string directory = "200423859 Junseob Noh";

            // To store StudentId, FirstName, and LastName
            student.FromDirectory(directory);

            string infoFilePath = student.FullPathUrl + "/" + Constants.Locations.InforFile;
            bool   fileExists   = FTPApp.FileExists(infoFilePath);

            if (fileExists == true)
            {
                byte[]   bytes    = FTPApp.DownloadFileBytes(infoFilePath);
                string   csvData  = Encoding.Default.GetString(bytes);
                string[] csvlines = csvData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
                if (csvlines.Length != 2)
                {
                    Console.WriteLine("Error in CSV format");
                }
                else
                {
                    student.FromCSV(csvlines[1]);
                    Console.WriteLine(">>> myimage.jpg file as Base64:");
                    Console.WriteLine(student.ImageData);
                }
            }
        }
예제 #2
0
        static void SaveFiles(List <Student> students, string fileName)
        {
            // 1. Save CSV file
            string studentCSVPath = $"{Constants.Locations.DataFolder}\\{fileName}.csv";

            //Establish a file stream to collect data from the response. this point is local disk.
            using (StreamWriter fs = new StreamWriter(studentCSVPath))
            {
                int headerNum = 0;
                foreach (var student in students)
                {
                    if (headerNum == 0)
                    {
                        fs.Write(student.HeaderRow + "\r\n");
                        headerNum++;
                    }
                    else
                    {
                        fs.Write(student.ToCSV() + "\r\n");
                    }
                }
            }

            // 2. Save JSON file, which is converted from CSV file
            string studentJSONPath = $"{Constants.Locations.DataFolder}\\{fileName}.json";
            string analyticsData   = CsvToJson.ReadFile(studentCSVPath);

            using (StreamWriter fs = new StreamWriter(studentJSONPath))
            {
                fs.Write(analyticsData);
            }

            // 3. Save XML file, which is converted from CSV file
            string    studentXMLPath = $"{Constants.Locations.DataFolder}\\{fileName}.xml";
            string    csv            = File.ReadAllText(studentCSVPath);
            XDocument doc            = CsvToXml.ConvertCsvToXML(csv, new[] { "," });

            doc.Save(studentXMLPath);

            // 4. Upload the files to My FTP
            foreach (var student in students)
            {
                if (student.MyRecord)
                {
                    FTPApp.UploadFile(studentCSVPath, student.FullPathUrl + "/students.csv");
                    FTPApp.UploadFile(studentJSONPath, student.FullPathUrl + "/students.json");
                    FTPApp.UploadFile(studentXMLPath, student.FullPathUrl + "/students.xml");
                }
            }
        }
예제 #3
0
        static List <Student> CollectStudentInfor()
        {
            List <Student> students = new List <Student>();

            // fetch the diretoreis info files and Images, and then display the console them.
            List <string> directories = FTPApp.GetDirectory(Constants.FTP.BaseUrl);

            foreach (var directory in directories)
            {
                Student student = new Student()
                {
                    AbsoluteUrl = Constants.FTP.BaseUrl
                };
                // To store StudentId, FirstName, and LastName
                student.FromDirectory(directory);

                string infoFilePath = student.FullPathUrl + "/" + Constants.Locations.InforFile;
                bool   fileExists   = FTPApp.FileExists(infoFilePath);
                if (fileExists == true)
                {
                    byte[]   bytes    = FTPApp.DownloadFileBytes(infoFilePath);
                    string   csvData  = Encoding.Default.GetString(bytes);
                    string[] csvlines = csvData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
                    if (csvlines.Length != 2)
                    {
                        Console.WriteLine($"{student.FirstName} {student.LastName} has Error in CSV format");
                    }
                    else
                    {
                        student.FromCSV(csvlines[1]);
                    }
                    if (student.StudentId == "200423859")
                    {
                        student.MyRecord = true;
                    }
                    else
                    {
                        student.MyRecord = false;
                    }
                }
                else
                {
                    Console.WriteLine($"{student.FirstName} {student.LastName} Could not find info file:");
                }
                students.Add(student);
            }

            return(students);
        }
예제 #4
0
        // Get all directories on the FTPApp for our class.
        static void GetFtpDirectories()
        {
            List <string> directories = FTPApp.GetDirectory(Constants.FTP.BaseUrl);
            int           loc         = 0;

            foreach (var directory in directories)
            {
                if (loc == 0)
                {
                    loc++;
                }
                else
                {
                    Console.WriteLine("\n" + directory);
                    List <string> files = FTPApp.GetDirectory(Constants.FTP.BaseUrl + "/" + directory);
                    foreach (var file in files)
                    {
                        Console.WriteLine("\t" + file);
                    }
                }
            }
        }