示例#1
0
        public ActionResult Upload(ImageInfo image, HttpPostedFileBase image_file)
        {
            CheckADA();
            if (ModelState.IsValid)
            {
                ApplicationUser user = getLoggedInUser();

                if (user != null)
                {
                    image.user_id = user.Id;
                    Image imageEntity = new Image(image);
                    imageEntity.SetUser(user);
                    //JavaScriptSerializer jss = new JavaScriptSerializer();
                    //String jsonData = jss.Serialize(image);
                    //String fileName = Server.MapPath("~/APP_Data/Image_INFO/" + image.image_id + ".js");

                    if (image_file != null && image_file.ContentLength > 0)
                    {
                        db.Images.Add(imageEntity);
                        db.SaveChanges();
                        imageEntity = db.Images.Find(imageEntity.id);

                        ImageStorage.saveFile(Server, image_file, imageEntity.id);

                        ViewBag.Title = "Upload Successful";
                        ImageInfo imagedto = new ImageInfo(imageEntity);
                        imagedto.TagName = db.Tags.Find(imageEntity.tagId).Name;
                        return(View("Detail", imagedto));
                    }
                    else
                    {
                        ViewBag.Message = "no image!";
                        return(View("Upload"));
                    }
                }
                else
                {
                    ViewBag.Message = "please register before upload";
                    return(RedirectToAction("Register", "Account"));
                }
            }
            else
            {
                return(View());
            }
        }