public ActionResult Create(BrandingIssue model) { HttpPostedFileBase file = Request.Files["ImageData"]; int i = UploadImageInDataBase(file, model); if (i == 1) { return(RedirectToAction("Index")); } return(View(model)); }
public int UploadImageInDataBase(HttpPostedFileBase file, BrandingIssue contentViewModel) { var _entities = new BrandOutletMonitoringEntities(); //contentViewModel.IssueFile = ConvertToBytes(file); var Content = new BrandingIssue { //BrandId = contentViewModel.BrandId, //Description = contentViewModel.Description, //SubCategoryid = contentViewModel.SubCategoryid, IssueFile = contentViewModel.IssueFile }; _entities.BrandingIssues.Add(Content); int i = _entities.SaveChanges(); if (i == 1) { return(1); } else { return(0); } }
public Result PostDescription() { var masterResult = new Result(); var result = new Result(); var brandingIssue = new BrandingIssue(); var brandingIssueMaster = new BrandIssueMaster(); const string initialPath = @"~/Content/UploadImage"; try { //****Posted Files //****Posted Files var httpPostedFile = HttpContext.Current.Request.Files; // ReSharper disable once AssignNullToNotNullAttribute var strings = HttpContext.Current.Request.Form.GetValues("desc").ToList(); var stringsAddress = HttpContext.Current.Request.Form.GetValues("add"); var stringPhone = HttpContext.Current.Request.Form.GetValues("phone"); var stringSubcategoryId = HttpContext.Current.Request.Form.GetValues("subcategoryid"); var stringCategoryId = HttpContext.Current.Request.Form.GetValues("categoryid"); //Strings Bind //string bind string description = null; string address = null; string phone = null; long?categoryid = null; long?subcategoryid = null; if (strings != null) { description = strings[0]; } if (stringsAddress != null) { address = stringsAddress[0]; } if (stringPhone != null) { phone = stringPhone[0]; } if (stringSubcategoryId != null) { subcategoryid = Convert.ToInt64(stringSubcategoryId[0]); } if (stringCategoryId != null) { categoryid = Convert.ToInt64(stringCategoryId[0]); } //****Posted Files //****Posted Files // Get the uploaded image from the Files collection // Get the uploaded image from the Files collection var bmEntity = new BrandOutletMonitoringEntities(); var issueToken = Guid.NewGuid(); brandingIssueMaster.IssueId = issueToken; brandingIssueMaster.CategoryId = categoryid; brandingIssueMaster.SubCategoryId = subcategoryid; brandingIssueMaster.Description = description; brandingIssueMaster.RetailersPhone = phone; brandingIssueMaster.RetailersAddress = address; brandingIssueMaster.Active = true; brandingIssueMaster.AddedDate = DateTime.Now; bmEntity.BrandIssueMasters.Add(brandingIssueMaster); masterResult.issuccess = bmEntity.SaveChanges(); if (masterResult.issuccess == 1) { result.error = false; result.message = "Uploaded Successfully"; if (HttpContext.Current.Request.Files.AllKeys.Any()) { for (var iCnt = 0; iCnt <= httpPostedFile.Count - 1; iCnt++) { var postedFile = httpPostedFile[iCnt]; var fileName = Path.GetFileName(postedFile.FileName); if (fileName != null) { var todayTime = DateTime.Now; var time = new DateTime(todayTime.Year, todayTime.Month, todayTime.Day, todayTime.Hour, todayTime.Minute, todayTime.Second, todayTime.Millisecond); var timeFormat = string.Format("{0:yyyy-MM-dd_hh-mm-ss-fff}", time); fileName = timeFormat + "-" + fileName; var path = Path.Combine(HttpContext.Current.Server.MapPath(initialPath), fileName); postedFile.SaveAs(path); string finalPath = @"C:\uploads" + @"\" + 1 + @"\" + 2 + @"\" + 3; if (Directory.Exists(finalPath)) { File.Copy(path, finalPath + "\\" + fileName, true); } else { Directory.CreateDirectory(finalPath); File.Copy(path, finalPath + "\\" + fileName, true); } brandingIssue.BrandIssueId = issueToken; brandingIssue.IssueFile = finalPath + "\\" + fileName; bmEntity.BrandingIssues.Add(brandingIssue); bmEntity.SaveChanges(); result.error = false; result.message = "Uploaded Successfully"; } } } } else { result.error = true; result.message = "Uploaded Failed"; } //var httpPostedFile = HttpContext.Current.Request.Files["upload"]; // } catch (Exception) { result.error = true; result.message = "Uploaded Failed"; } return(result); }