コード例 #1
0
/*
 *      private ModelStateDictionary _modelState;
 *
 *      public RecipServices(ModelStateDictionary dictionary)
 *      {
 *          _modelState = dictionary;
 *      }
 *
 *      public bool ValidateFile(AddNewRecip modelToValidate)
 *      {
 *          if (modelToValidate.File.ContentLength < ValidateDefinitions.maxSizeOfFile)
 *              _modelState.AddModelError(ValidateDefinitions.nameOfField, ValidateDefinitions.sizeOfFile);
 *          if (!(modelToValidate.File.ContentType == ValidateDefinitions.firstOfType || modelToValidate.File.ContentType == ValidateDefinitions.secondOfType))
 *              _modelState.AddModelError(ValidateDefinitions.nameOfField,ValidateDefinitions.validFormatOfFile);
 *          return _modelState.IsValid;
 *      }
 */

        public void addNewRecip(AddNewRecip model)
        {
            var receipModel = new RecipModel();

            using (ApplicationDBContext ctx = new ApplicationDBContext())
            {
                receipModel.Title            = model.Title;
                receipModel.Content          = model.Content;
                receipModel.ShortDescription = model.ShortDescription;
                receipModel.FileName         = model.File.FileName;
                receipModel.ImageSize        = model.File.ContentLength;
                byte[] data = new byte[model.File.ContentLength];
                model.File.InputStream.Read(data, 0, model.File.ContentLength);
                receipModel.ImageData = data;
                ctx.Recipts.Add(receipModel);
                ctx.SaveChanges();
            }
        }
コード例 #2
0
 public ActionResult Index(AddNewRecip model)
 {
     if (model.File.ContentLength > (2 * 1024 * 1024))
     {
         ModelState.AddModelError("CustomError", "Plik musi być mniejszy niż 2MB");
     }
     if (!(model.File.ContentType == "image/jpeg" || model.File.ContentType == "image/gif"))
     {
         ModelState.AddModelError("CustomError", "Plik jest w złym formacie!");
     }
     if (ModelState.IsValid)
     {
         serv.addNewRecip(model);
         ViewBag.Message = "Uzupelnij poprawnie pola formularza.";
         return(RedirectToAction("Index"));
     }
     return(View(new AddNewRecip()));
 }