public ActionResult UploadFile(PhotoBindingModel model) { if (ModelState.IsValid) { if (model.Upload != null) { var paths = Helpers.UploadFiles.UploadImage(model.Upload, false); var photo = new Picture { ImageURL = DropBox.Download(paths[0]), Owner = this.UserProfile }; this.UserProfile.Pictures.Add(photo); this.Data.SaveChanges(); this.TempData["Success"] = new[] { "Edit successfull" }; } return this.View(); } return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid model"); }
public ActionResult Upload(HttpPostedFileBase file, int contestId) { var currentUserId = this.User.Identity.GetUserId(); var contest = this.Data.Contests.All() .FirstOrDefault(c => c.Id == contestId); try { /*Geting the file name*/ string filename = System.IO.Path.GetFileName(file.FileName); /*Saving the file in server folder*/ file.SaveAs(Server.MapPath("~/Images/" + filename)); /*Saving the fileURL in database*/ var picture = new Picture { ImageURL = filename, OwnerId = currentUserId, Contests = new List<Contest>{contest} }; this.Data.Pictures.Add(picture); this.Data.SaveChanges(); ViewBag.Message = "File Uploaded successfully."; } catch { ViewBag.Message = "Error while uploading the files."; } return View(); }