示例#1
0
        public ActionResult UploadImage(string FileUpload, string DuplicateId, int Num)
        {
            if (!(Session["IsAdmin"] != null && Session["IsAdmin"].ToString().ToUpper() == "TRUE"))
            {
                return(Content("Not allowed", System.Net.Mime.MediaTypeNames.Text.Plain));
            }


            using (var ctx = new WordsContext())
            {
                using (var image = System.Drawing.Image.FromStream(Request.Files[0].InputStream))
                {
                    using (var scaledImage = ImageController.ScaleImage(image, 300, 300))
                    {
                        MemoryStream ms = new MemoryStream();
                        scaledImage.Save(ms, ImageFormat.Jpeg);

                        var dup = ctx.Duplicates.Where(x => x.DuplicateId.ToString() == DuplicateId).SingleOrDefault();
                        switch (Num)
                        {
                        case 0:
                            dup.ImageQuestion         = new Picture();
                            dup.ImageQuestion.Content = ms.ToArray();
                            break;

                        case 1:
                            dup.Image1         = new Picture();
                            dup.Image1.Content = ms.ToArray();
                            break;

                        case 2:
                            dup.Image2         = new Picture();
                            dup.Image2.Content = ms.ToArray();
                            break;

                        case 3:
                            dup.Image3         = new Picture();
                            dup.Image3.Content = ms.ToArray();
                            break;

                        case 4:
                            dup.Image4         = new Picture();
                            dup.Image4.Content = ms.ToArray();
                            break;
                        }


                        ctx.ChangeTracker.DetectChanges();
                        ctx.SaveChanges();
                    }
                }
            }

            return(Json(new { success = "Valid" }));
        }
示例#2
0
        public ActionResult UploadImage(string FileUpload, string WordId, string CollectionId)
        {
            if (!(Session["IsAdmin"] != null && Session["IsAdmin"].ToString().ToUpper() == "TRUE"))
            {
                return(Content("Not allowed", System.Net.Mime.MediaTypeNames.Text.Plain));
            }


            using (var ctx = new WordsContext())
            {
                using (var image = System.Drawing.Image.FromStream(Request.Files[0].InputStream))
                {
                    using (var scaledImage = ImageController.ScaleImage(image, 300, 300))
                    {
                        MemoryStream ms = new MemoryStream();
                        scaledImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        if (CollectionId == "undefined")
                        {
                            var word = ctx.Words.Where(x => x.WordId.ToString() == WordId).SingleOrDefault();
                            word.Image         = new Picture();
                            word.Image.Content = ms.ToArray();
                        }
                        else
                        {
                            var collection = ctx.Collections.Where(x => x.CollectionId.ToString() == CollectionId).SingleOrDefault();
                            collection.Image         = new Picture();
                            collection.Image.Content = ms.ToArray();
                        }

                        ctx.ChangeTracker.DetectChanges();
                        ctx.SaveChanges();
                    }
                }
            }

            return(Json(new { success = "Valid" }));
        }