コード例 #1
0
        public ActionResult Upload(HttpPostedFileBase upload)
        {
            if (upload == null)
            {
                return(View("Error"));
            }

            using (var context = new ProfileSampleEntities())
            {
                using (var stream = upload.InputStream)
                {
                    byte[] buff = new byte[stream.Length];

                    stream.Read(buff, 0, (int)stream.Length);

                    var entity = new ImgSource()
                    {
                        Name = upload.FileName,
                        Data = buff,
                    };

                    context.ImgSources.Add(entity);
                    context.SaveChanges();
                }
            }

            return(RedirectToAction("Index", "Home"));
        }
コード例 #2
0
        public ActionResult Convert()
        {
            var files = Directory.GetFiles(Server.MapPath("~/Content/Img"), "*.jpg");

            using (var context = new ProfileSampleEntities())
            {
                foreach (var file in files)
                {
                    using (var stream = new FileStream(file, FileMode.Open))
                    {
                        byte[] buff = new byte[stream.Length];

                        stream.Read(buff, 0, (int)stream.Length);

                        var entity = new ImgSource()
                        {
                            Name = Path.GetFileName(file),
                            Data = buff,
                        };

                        context.ImgSources.Add(entity);
                        context.SaveChanges();
                    }
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public ActionResult Index()
        {
            var context = new ProfileSampleEntities();

            var sources = context.ImgSources.Take(20).Select(x => new ImageModel {
                Name = x.Name
            });

            //var model = new List<ImageModel>();

            //foreach (var id in sources)
            //{
            //    var item = context.ImgSources.Find(id);

            //    var obj = new ImageModel()
            //    {
            //        Name = item.Name,
            //        Data = item.Data
            //    };

            //    model.Add(obj);
            //}

            return(View(sources.ToList()));
        }
コード例 #4
0
        //public ActionResult Index()
        //{
        //    var context = new ProfileSampleEntities();

        //    var sources = context.ImgSources.Take(20).Select(x => x.Id);

        //    var model = new List<ImageModel>();

        //    foreach (var id in sources)
        //    {
        //        var item = context.ImgSources.Find(id);

        //        var obj = new ImageModel()
        //        {
        //            Name = item.Name,
        //            Data = item.Data
        //        };

        //        model.Add(obj);
        //    }

        //    return View(model);
        //}

        public ActionResult Index()
        {
            var context = new ProfileSampleEntities();

            var sources = context.ImgSources.Take(20).Select(x => new ImageModel
            {
                Name = x.Name,
                Data = x.Data
            }).ToList();

            return(View(sources));
        }
コード例 #5
0
        public ActionResult Index()
        {
            var context = new ProfileSampleEntities();
            var models  = context.ImgSources //have done some optimization here
                          .Select(x => new ImageModel
            {
                Data = x.Data,
                Name = x.Name
            })
                          .Take(20)
                          .ToList();

            return(View(models));
        }
コード例 #6
0
        public ActionResult Index()
        {
            using (var context = new ProfileSampleEntities())
            {
                var sources = context.ImgSources.Take(20);

                var model = sources.Select(item => new ImageModel()
                {
                    Name = item.Name,
                    Data = item.Data
                }).ToList();

                return(View(model));
            }
        }
コード例 #7
0
        public ActionResult Get(string searchPhrase)
        {
            IEnumerable <ImageModel> sources;

            using (var context = new ProfileSampleEntities())
            {
                sources = context.ImgSources.Where(item => item.Name.StartsWith(searchPhrase))
                          .Select(item => new ImageModel()
                {
                    Name = item.Name,
                    Data = item.Data
                }).ToList();
            }

            return(PartialView(sources));
        }
コード例 #8
0
        public async Task <ActionResult> IndexOptimized()
        {
            var model = new List <NewImageModel>();

            using (var context = new ProfileSampleEntities())
            {
                var source = await context.ImgSources.Take(20).ToArrayAsync();

                model = source.Select(x => new NewImageModel()
                {
                    Name = x.Name,
                    Data = string.Format("data:image/jpg;base64,{0}", System.Convert.ToBase64String(x.Data))
                }).ToList();
            }

            return(View(model));
        }
コード例 #9
0
        public ActionResult Convert()
        {
            using (var context = new ProfileSampleEntities())
            {
                foreach (var file in Directory.EnumerateFiles(Server.MapPath("~/Content/Img"), "*.jpg"))
                {
                    var buff = System.IO.File.ReadAllBytes(file);

                    context.ImgSources.Add(new ImgSource()
                    {
                        Name = Path.GetFileName(file),
                        Data = buff,
                    });

                    context.SaveChanges();
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #10
0
        public ActionResult Convert()
        {
            var files = Directory.GetFiles(Server.MapPath("~/Content/Img"), "*.jpg");

            using (var context = new ProfileSampleEntities())
            {
                foreach (var contextImgSource in context.ImgSources.ToList())
                {
                    context.ImgSources.Remove(contextImgSource);
                }
                files.ForEach(file => context.ImgSources.Add(new ImgSource()
                {
                    Name = Path.GetFileName(file),
                    Data = LowerImageQuality(System.IO.File.ReadAllBytes(file), 10)
                }));

                context.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
コード例 #11
0
        public ActionResult Index()
        {
            var context = new ProfileSampleEntities();

            var sources = context.ImgSources.Take(20);

            var model = new List <ImageModel>();

            foreach (var item in sources)
            {
                var obj = new ImageModel()
                {
                    Name = item.Name,
                    Data = item.Data
                };

                model.Add(obj);
            }

            return(View(model));
        }
コード例 #12
0
        public ActionResult Original()
        {
            var context = new ProfileSampleEntities();

            var sources = context.ImgSources.Take(20).Select(x => x.Id);

            var model = new List <ImageModel>();

            foreach (var id in sources)
            {
                var item = context.ImgSources.Find(id);

                var obj = new ImageModel()
                {
                    Name = item.Name,
                    Data = item.Data
                };

                model.Add(obj);
            }

            return(View("Index", model));
        }
コード例 #13
0
 public HomeController()
 {
     _context = new ProfileSampleEntities();
 }