Exemple #1
0
        public ActionResult Create(News news, IEnumerable<HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {
                string filename = "";
                byte[] bytes;
                int BytestoRead;
                int numBytesRead;
                Image image;

                //get next id
                var rows = db.News.ToList();

                var nextId = 0;
                if (rows.Count > 0)
                {
                    var lastRow = rows[rows.Count - 1];
                    var lastId = lastRow.ID;
                    nextId = lastId + 1;
                }
                else
                {
                    nextId++;
                }

                //save news
                db.News.Add(news);
                db.SaveChanges();

                if (files != null)
                {
                    foreach (var file in files)
                    {
                        filename = Path.GetFileName(file.FileName);
                        bytes = new byte[file.ContentLength];
                        BytestoRead = (int)file.ContentLength;
                        numBytesRead = 0;
                        while (BytestoRead > 0)
                        {
                            int n = file.InputStream.Read(bytes, numBytesRead, BytestoRead);
                            if (n == 0) break;
                            numBytesRead += n;
                            BytestoRead -= n;
                        }

                        //save image
                        image = new Image();
                        image.ImageFile = bytes;
                        image.NewsID = nextId;
                        image.Ext = "?";

                        db.Images.Add(image);
                        db.SaveChanges();
                    }
                }
                return RedirectToAction("index");
            }

            return View();
        }
Exemple #2
0
        public ActionResult Edit(News news, IEnumerable<HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {
                db.Entry(news).State = EntityState.Modified;
                db.SaveChanges();

                string filename = "";
                byte[] bytes;
                int BytestoRead;
                int numBytesRead;
                string ext;
                Image image;

                int id = news.ID;

                if (files != null)
                {
                    foreach (var file in files)
                    {
                        filename = Path.GetFileName(file.FileName);
                        bytes = new byte[file.ContentLength];
                        BytestoRead = (int)file.ContentLength;
                        numBytesRead = 0;
                        ext = System.IO.Path.GetExtension(file.FileName);
                        while (BytestoRead > 0)
                        {
                            int n = file.InputStream.Read(bytes, numBytesRead, BytestoRead);
                            if (n == 0) break;
                            numBytesRead += n;
                            BytestoRead -= n;
                        }

                        //save image
                        image = new Image();
                        image.ImageFile = bytes;
                        image.NewsID = id;
                        image.Ext = ext;

                        db.Images.Add(image);
                        db.SaveChanges();
                    }
                }

                return RedirectToAction("Watch", "News", new { id = news.ID });
            }
            return View(news);
        }