// GET: SaveImageProduct
        public ActionResult Index()
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    int count = 0;
                    var list  = _context.ProductModel.Where(p => p.FileId == null && p.ImageUrl != "noimage.jpg").ToList();
                    foreach (var item in list)
                    {
                        string filepath = Server.MapPath("/Upload/Product/Thum/" + item.ImageUrl);
                        if (System.IO.File.Exists(filepath) && item.ImageUrl != "noimage.jpg")
                        {
                            string filename    = item.ImageUrl;
                            int    index       = filename.IndexOf(".");
                            string type        = filename.Substring(index);
                            string type2       = filename.Substring(index + 1);
                            string ContentType = "Image/" + type2;

                            Image myImg = Image.FromFile(filepath);
                            //System.IO.FileInfo info = new System.IO.FileInfo(filepath);

                            byte[] FileContent = imageToByteArray(myImg);
                            int    size        = FileContent.Length;

                            //addmodel.ImageUrl = Upload(file, "Product");

                            SYS_tblFile FileSave = new SYS_tblFile()
                            {
                                FileTitle       = filename,
                                FileName        = item.ImageUrl,
                                Extension       = type,
                                ContentType     = ContentType,
                                FileContent     = FileContent,
                                FolderId        = 1,
                                Size            = size,
                                CreatedByUserId = currentAccount.EmployeeId,
                                CreatedOnDate   = DateTime.Now
                            };
                            _context.Entry(FileSave).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                            item.FileId = FileSave.FileId;
                            _context.Entry(item).State = System.Data.Entity.EntityState.Modified;
                            _context.SaveChanges();
                            count++;
                        }
                    }
                    ViewBag.Message = "Sao lưu  thành công " + count + " file hình ảnh !";
                    ts.Complete();
                }
            }
            catch
            {
                ViewBag.Message = "Sao lưu không thành công , lỗi trong quá trình sao lưu hình ảnh !";
            }
            return(View());
        }
Esempio n. 2
0
        public ActionResult Create(Website_NewsModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    // Lưu Website_NewsModel
                    model.UserId   = currentAccount.EmployeeId;
                    model.PostDate = DateTime.Now;
                    model.Views    = 0;
                    model.SEOTitle = Library.ConvertToNoMarkString(model.Title);
                    if (file != null)
                    {
                        string filename = file.FileName;
                        int    index    = filename.IndexOf(".");
                        string type     = filename.Substring(index);

                        int    size        = file.ContentLength;
                        string ContentType = file.ContentType;

                        byte[] FileContent = imageToByteArray(file.InputStream);
                        model.ImageUrl = Upload(file, "Website_News");

                        //lưu
                        SYS_tblFile FileSave = new SYS_tblFile()
                        {
                            FileTitle       = filename,
                            FileName        = model.ImageUrl,
                            Extension       = type,
                            ContentType     = ContentType,
                            FileContent     = FileContent,
                            FolderId        = 2,
                            Size            = size,
                            CreatedByUserId = model.UserId,
                            CreatedOnDate   = model.PostDate
                        };
                        _context.Entry(FileSave).State = System.Data.Entity.EntityState.Added;
                        _context.SaveChanges();
                        model.FileId = FileSave.FileId;
                    }
                    model.Actived = true;
                    _context.Entry(model).State = System.Data.Entity.EntityState.Added;
                    _context.SaveChanges();
                    ts.Complete();
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                CreateViewBag(null);
                return(View(model));
            }
        }