コード例 #1
0
        public ActionResult Create(int id)
        {
            var seal = new SealDocument()
            {
                DocumentId = id,
            };

            return(View(seal));
        }
コード例 #2
0
        public ActionResult Create(SealDocument Seal, IEnumerable <HttpPostedFileBase> SealFiles)
        {
            if (SealFiles != null)
            {
                foreach (var file in SealFiles)
                {
                    if (!CheckFileFormatting.PermissionFile(file))
                    {
                        ModelState.AddModelError("File", "صيغة الملف غير مدعومة!");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                Seal.CreatedAt   = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss");
                Seal.CreatedById = User.Identity.GetUserId();

                // Encrypt Document Attributes.
                if (ManagedAes.IsCipher)
                {
                    Seal.CreatedAt = ManagedAes.EncryptText(Seal.CreatedAt);
                    Seal.Message   = ManagedAes.EncryptText(Seal.Message);
                }

                _context.SealDocuments.Add(Seal);
                _context.SaveChanges();

                if (ManagedAes.IsSaveInDb)
                {
                    // start code get image from scanner, save it in database.
                    var scannedImages = Request.Form.GetValues("myfile");
                    if (scannedImages != null)
                    {
                        int i = 0;
                        foreach (var ImgStr in scannedImages)
                        {
                            i++;
                            var sealFiles = new SealFiles();

                            sealFiles.SealId = Seal.Id;

                            string imageName = "scannedImage" + i + ".jpg";

                            var imgAsByteArray = ManagedAes.DecodeUrlBase64(ImgStr);

                            if (ManagedAes.IsCipher)
                            {
                                // Encrypt File Name.
                                sealFiles.FileName = ManagedAes.EncryptText(imageName);
                                // Encrypt File.
                                var EncryptedImgAsByteArray = ManagedAes.EncryptArrayByte(imgAsByteArray);
                                sealFiles.File = new byte[EncryptedImgAsByteArray.Length];
                                sealFiles.File = EncryptedImgAsByteArray;
                            }
                            else
                            {
                                // File Name.
                                sealFiles.FileName = imageName;
                                // File.
                                sealFiles.File = new byte[imgAsByteArray.Length];
                                sealFiles.File = imgAsByteArray;
                            }

                            _context.SealFiles.Add(sealFiles);
                            _context.SaveChanges();
                        }
                    }
                    // .// end code // get image from scanner, save it in server.


                    foreach (HttpPostedFileBase file in SealFiles)
                    {
                        if (file != null)
                        {
                            var sealFiles = new SealFiles();

                            sealFiles.SealId = Seal.Id;

                            string FileName = Path.GetFileName(file.FileName);
                            sealFiles.FileName = FileName;

                            sealFiles.File = new byte[file.ContentLength];
                            file.InputStream.Read(sealFiles.File, 0, file.ContentLength);

                            if (ManagedAes.IsCipher)
                            {
                                // Encrypt File Name.
                                sealFiles.FileName = ManagedAes.EncryptText(FileName);
                                // Encrypt File.
                                sealFiles.File = ManagedAes.EncryptArrayByte(sealFiles.File);
                            }

                            _context.SealFiles.Add(sealFiles);
                            _context.SaveChanges();
                        }
                    }
                }
                else
                {
                    // start code get image from scanner, save it in server
                    var scannedImages = Request.Form.GetValues("myfile");
                    if (scannedImages != null)
                    {
                        int i = 0;
                        foreach (var ImgStr in scannedImages)
                        {
                            i++;
                            String path = Server.MapPath("~/Uploads"); //Path

                            //Check if directory exist
                            if (!System.IO.Directory.Exists(path))
                            {
                                System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
                            }
                            string imageName = "scannedImage" + i + ".jpg";
                            string s1        = DateTime.Now.ToString("yyyyMMddhhHHmmss") + imageName;

                            //set the image path
                            string imgPath    = Path.Combine(path, imageName);
                            byte[] imageBytes = ManagedAes.DecodeUrlBase64(ImgStr);

                            var sealFiles = new SealFiles();
                            sealFiles.SealId = Seal.Id;

                            if (ManagedAes.IsCipher)
                            {
                                ManagedAes.EncryptFile(imageBytes, imgPath);
                                imageName = ManagedAes.EncryptText(imageName);
                                s1        = ManagedAes.EncryptText(s1);
                            }
                            else
                            {
                                System.IO.File.WriteAllBytes(imgPath, imageBytes);
                            }

                            sealFiles.FileName = imageName;

                            sealFiles.FileUrl = s1;

                            _context.SealFiles.Add(sealFiles);
                            _context.SaveChanges();
                        }
                    }
                    // .// end code // get image from scanner, save it in server.

                    foreach (var file in SealFiles)
                    {
                        if (file != null)
                        {
                            var sealFiles = new SealFiles();
                            sealFiles.SealId = Seal.Id;

                            //Save File In Uploads
                            string FileName = Path.GetFileName(file.FileName);

                            string s1   = DateTime.Now.ToString("yyyyMMddhhHHmmss") + FileName;
                            string path = Path.Combine(Server.MapPath("~/Uploads"), s1);

                            if (ManagedAes.IsCipher)
                            {
                                ManagedAes.EncryptFile(file, path);
                                FileName = ManagedAes.EncryptText(FileName);
                                s1       = ManagedAes.EncryptText(s1);
                            }
                            else
                            {
                                file.SaveAs(path);
                            }

                            sealFiles.FileName = FileName;

                            sealFiles.FileUrl = s1;

                            _context.SealFiles.Add(sealFiles);
                            _context.SaveChanges();
                        }
                    }
                }

                return(RedirectToAction("Index", new { id = Seal.DocumentId }));
            }
            return(View(Seal));
        }