コード例 #1
0
        public ActionResult Create(CreateImageViewModel image)
        {
            if (ModelState.IsValid)
            {
                SaveFile(image);
                var imageToSave = new Image() { MIME = image.MIME, Id = image.Id, Title = image.Title, Url = image.Url };
                db.Images.Add(imageToSave);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(image);
        }
コード例 #2
0
        private void SaveFile(CreateImageViewModel image)
        {
            var fileName = image.HttpPostedFileBase.FileName.Split('\\').Last();
            image.Url = UploadFolder + fileName;
            image.MIME = image.HttpPostedFileBase.ContentType;
            var filePath = Server.MapPath(image.Url);

            using (var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                image.HttpPostedFileBase.InputStream.CopyTo(fileStream);
            }
        }