Exemplo n.º 1
0
        public bool FileDbUpload(string fileName)
        {
            const string LOCALPATHROOTH = "D:\\App\\TextFileDemoApp\\TextFileDemoApp\\bin\\Debug\\";
            string       localPath      = $@"{LOCALPATHROOTH}{fileName}";

            if (File.Exists(localPath))
            {
                var db = new FiledbEntities();

                var fileModel = new SerializedFileDto
                {
                    Name        = fileName.Split('.')[0],
                    Extension   = fileName.Split('.')[1],
                    FileContent = File.ReadAllText(localPath),
                };

                var file = SerializedFileDto.MapTo(fileModel);

                db.SerializedFiles.Add(file);
                db.SaveChanges();
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public IActionResult DbDownload(IEnumerable <int> ids, IEnumerable <FileExtEnum> ext)
        {
            List <SerializedFileDto> dtoCheckedFileList = _context.SerializedFile.Where(x => ids.Contains(x.Id)).ToList();
            List <SerializedFile>    checkedFileList    = new List <SerializedFile>();

            foreach (SerializedFileDto serializedFileDto in dtoCheckedFileList)
            {
                SerializedFile serializedFile = SerializedFileDto.MapTo(serializedFileDto);
                serializedFile.Extension = "." + ext.ToString();
                checkedFileList.Add(serializedFile);
            }

            if (checkedFileList.Count == 0)
            {
                return(RedirectToAction("CheckDelete"));
            }

            if (checkedFileList.Count == 1)
            {
                //int checkedItemIndex = fileGridView.SelectedCells[0].RowIndex;
                //DataGridViewRow selectedRow = fileGridView.Rows[checkedItemIndex];

                //var checkedItem = Convert.ToString(selectedRow.Cells["FileName"].Value);
                //var checkedExt = "." + Convert.ToString(selectedRow.Cells["DownloadFormat"].Value);

                //var db = new FiledbEntities();
                //var checkedItemContent = db.SerializedFiles.FirstOrDefault(x =>
                //    x.Name == checkedItem)?.FileContent;

                //SerializedFile file = _fileSerialization.CreateFile(SerializedFile., checkedExt, checkedItemContent);

                switch (ext)
                {
                    //case FileExtEnum.xml:
                    //    _fileSerialization.XmlSerializeToFile(SerializedFileDto.MapTo(checkedFileList[0]));

                    //case ".json":
                    //    _fileSerialization.JsonSerializeToFile(SerializedFileDto.MapTo(file));
                    //    MessageBox.Show(@"Json serialized file downloaded");
                    //    break;
                    //case ".bin":
                    //    _fileSerialization.BinarySerializeToFile(SerializedFileDto.MapTo(file));
                    //    MessageBox.Show(@"Bin serialized file downloaded");
                    //    break;
                    //default:
                    //    MessageBox.Show(@"Please select a format to download");
                    //    break;
                }
                return(RedirectToAction("CheckDelete"));
            }

            return(RedirectToAction("CheckDelete"));
        }
Exemplo n.º 3
0
        public IActionResult CheckDelete(IEnumerable <int> ids)
        {
            List <SerializedFileDto> dtoCheckedFileList = _context.SerializedFile.Where(x => ids.Contains(x.Id)).ToList();
            List <SerializedFile>    checkedFileList    = new List <SerializedFile>();

            foreach (SerializedFileDto serializedFileDto in dtoCheckedFileList)
            {
                SerializedFile serializedFile = SerializedFileDto.MapTo(serializedFileDto);
                checkedFileList.Add(serializedFile);
            }

            _fileDbService.FileDelete(checkedFileList);

            return(View());
        }
Exemplo n.º 4
0
        private void BtnDbDownload(object sender, EventArgs e)
        {
            if (fileGridView.Rows.Count == 0)
            {
                MessageBox.Show(@"Please upload a file to dB!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int checkedItemsNb = GetCheckedItemsNo();

            if (checkedItemsNb == 1)
            {
                int             checkedItemIndex = fileGridView.SelectedCells[0].RowIndex;
                DataGridViewRow selectedRow      = fileGridView.Rows[checkedItemIndex];

                var checkedItem = Convert.ToString(selectedRow.Cells["FileName"].Value);
                var checkedExt  = "." + Convert.ToString(selectedRow.Cells["DownloadFormat"].Value);

                var db = new FiledbEntities();
                var checkedItemContent = db.SerializedFiles.FirstOrDefault(x =>
                                                                           x.Name == checkedItem)?.FileContent;

                SerializedFile file = _fileSerialization.CreateFile(checkedItem, checkedExt, checkedItemContent);

                switch (checkedExt)
                {
                case ".xml":
                    _fileSerialization.XmlSerializeToFile(SerializedFileDto.MapTo(file));
                    MessageBox.Show(@"Xml serialized file downloaded");
                    break;

                case ".json":
                    _fileSerialization.JsonSerializeToFile(SerializedFileDto.MapTo(file));
                    MessageBox.Show(@"Json serialized file downloaded");
                    break;

                case ".bin":
                    _fileSerialization.BinarySerializeToFile(SerializedFileDto.MapTo(file));
                    MessageBox.Show(@"Bin serialized file downloaded");
                    break;

                default:
                    MessageBox.Show(@"Please select a format to download");
                    break;
                }
            }

            else if (checkedItemsNb > 1)
            {
                var checkedItemsList = GetCheckedItemsList();

                List <SerializedFile> serializedItemsList = new List <SerializedFile>();
                foreach (var file in checkedItemsList)
                {
                    string checkedExt = file.Extension;
                    switch (checkedExt)
                    {
                    case ".xml":
                        SerializedFile xmlSerializedFile = _fileSerialization.XmlSerializeToFile(SerializedFileDto.MapTo(file));
                        serializedItemsList.Add(xmlSerializedFile);

                        break;

                    case ".json":
                        SerializedFile jsonSerializedFile = _fileSerialization.JsonSerializeToFile(SerializedFileDto.MapTo(file));
                        serializedItemsList.Add(jsonSerializedFile);

                        break;

                    case ".bin":
                        SerializedFile binSerializedFile = _fileSerialization.JsonSerializeToFile(SerializedFileDto.MapTo(file));
                        serializedItemsList.Add(binSerializedFile);

                        break;

                    default:
                        MessageBox.Show(@"Please select a format to download");
                        return;
                    }
                }

                bool isDownloaded = _fileDbService.ZipFileDbDownload(serializedItemsList);
                if (isDownloaded)
                {
                    MessageBox.Show(@"Zip download done");
                    return;
                }
                else
                {
                    MessageBox.Show(@"Please Select a file to download");
                    return;
                }
            }
            else
            {
                MessageBox.Show(@"Please select a file to download");
            }
        }