public virtual JsonResult <UploadImageResult> UploadImage() { var memberService = UnityHelper.Container.Resolve <IMembershipService>(); var roleService = UnityHelper.Container.Resolve <IRoleService>(); var localizationService = UnityHelper.Container.Resolve <ILocalizationService>(); var uploadService = UnityHelper.Container.Resolve <IUploadedFileService>(); var context = UnityHelper.Container.Resolve <IMvcForumContext>(); var loggingService = UnityHelper.Container.Resolve <ILoggingService>(); roleService.RefreshContext(context); uploadService.RefreshContext(context); localizationService.RefreshContext(context); memberService.RefreshContext(context); try { if (HttpContext.Current.Request.Files.AllKeys.Any()) { // Get the uploaded image from the Files collection var httpPostedFile = HttpContext.Current.Request.Files["file"]; if (httpPostedFile != null) { HttpPostedFileBase photo = new HttpPostedFileWrapper(httpPostedFile); memberService.GetUser(HttpContext.Current.User.Identity.Name); var permissions = roleService.GetPermissions(null, LoggedOnReadOnlyUser.Roles.FirstOrDefault()); // Get the permissions for this Group, and check they are allowed to update if (permissions[ForumConfiguration.Instance.PermissionInsertEditorImages].IsTicked) { // woot! User has permission and all seems ok // Before we save anything, check the user already has an upload folder and if not create one var uploadFolderPath = HostingEnvironment.MapPath( string.Concat(ForumConfiguration.Instance.UploadFolderPath, LoggedOnReadOnlyUser?.Id)); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } // If successful then upload the file var uploadResult = photo.UploadFile(uploadFolderPath, localizationService); if (!uploadResult.UploadSuccessful) { return(Json(new UploadImageResult())); } // Add the filename to the database var uploadedFile = new UploadedFile { Filename = uploadResult.UploadedFileName, MembershipUser = LoggedOnReadOnlyUser }; uploadedFile.DateCreated = DateTime.UtcNow; var user = context.MembershipUser.FirstOrDefault(x => x.Id == LoggedOnReadOnlyUser.Id); uploadedFile.MembershipUser = user; context.UploadedFile.Add(uploadedFile); // Commit the changes context.SaveChanges(); var result = new UploadImageResult { location = uploadResult.UploadedFileUrl }; return(Json(result)); } } } } catch (Exception ex) { context.RollBack(); loggingService.Error(ex); } return(Json(new UploadImageResult())); }