コード例 #1
0
ファイル: AdminController.cs プロジェクト: BelaGirl/Bakery
        public async Task <HttpResponseMessage> PostFile(int id)
        {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string root     = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ImageFolder"]);
            var    provider = new MultipartFormDataStreamProvider(root);

            try
            {
                StringBuilder sb = new StringBuilder();

                // Read the form data and return an async task.
                await Request.Content.ReadAsMultipartAsync(provider);

                var upload = HttpContext.Current.Request.Files[0];

                string fileName = Path.GetFileName(upload.FileName);

                var product = new ProductViewModel();
                product.Name  = provider.FormData.Get("name");
                product.Price = decimal.Parse(provider.FormData.Get("price"));
                product.CatId = id;

                string category = _service.GetAllCategories().First(c => c.Id == product.CatId).Name;

                product.Image = sb.Append(category).Append(@"\").Append(fileName).ToString();


                // папка для данной категории
                string pathForImage = root + category;
                // полное имя файла картинки
                string fullFileNameForImage = root + product.Image;

                if (!Directory.Exists(pathForImage))
                {
                    Directory.CreateDirectory(pathForImage);
                }

                upload.SaveAs(fullFileNameForImage);

                _service.CreateProduct(product);

                File.Delete(provider.FileData[0].LocalFileName);


                return(new HttpResponseMessage(HttpStatusCode.Created));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
コード例 #2
0
ファイル: AdminController.cs プロジェクト: BelaGirl/Bakery
        public ActionResult Create(CreateProductViewModel createProd, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid && upload != null)
            {
                string path = HttpContext.Request.UrlReferrer.AbsolutePath;

                ProductViewModel prod = new ProductViewModel();
                prod.Name  = createProd.Name;
                prod.Price = createProd.Price;
                prod.CatId = Convert.ToInt32(path.Split('/').Last());                                    // find id in the route
                string fileName = System.IO.Path.GetFileName(upload.FileName);


                string category = _service.GetAllCategories().First(c => c.Id == prod.CatId).Name;


                StringBuilder sb = new StringBuilder();
                prod.Image = sb.Append(category).Append(@"\").Append(fileName).ToString();
                _service.CreateProduct(prod);

                // директория для картинок
                string mainFolder = HttpContext.Server.MapPath(ConfigurationManager.AppSettings["ImageFolder"]);
                // папка для данной категории
                string pathForImage = mainFolder + category;
                // полное имя файла картинки
                string fullFileNameForImage = mainFolder + prod.Image;

                if (!System.IO.Directory.Exists(pathForImage))
                {
                    System.IO.Directory.CreateDirectory(pathForImage);
                }

                upload.SaveAs(fullFileNameForImage);

                return(Content(path));
            }


            return(PartialView(createProd));
        }
コード例 #3
0
ファイル: HomeController.cs プロジェクト: BelaGirl/Bakery
 public ActionResult Index()
 {
     return(View(_service.GetAllCategories()));
 }