public async Task <IActionResult> Edit([FromForm] UploadedFileModel model) { if (!ModelState.IsValid) { PartialView(); } if (model.FileToUpload != null) { //var uploads = Path.Combine(env.WebRootPath, "uploads"); var uploads = Path.Combine("C:/", "uploads"); bool exists = Directory.Exists(uploads); if (!exists) { Directory.CreateDirectory(uploads); } var fileName = Path.GetFileName(model.FileToUpload.FileName); var fileStream = new FileStream(Path.Combine(uploads, model.FileToUpload.FileName), FileMode.Create); string mimeType = model.FileToUpload.ContentType; byte[] fileData = new byte[model.FileToUpload.Length]; BlobStorageService objBlobService = new BlobStorageService(); model.ImageFilePath = objBlobService.UploadFileToBlob(model.ImageFilename, model.FileToUpload.FileName, fileData, mimeType); } // Map model to resource var resource = mapper.Map <UploadedFileResource>(model); // save resource to Json var resourceDocument = JsonConvert.SerializeObject(resource); using (var content = new StringContent(resourceDocument, Encoding.UTF8, "application/json")) { // determen call update or insert Upsert upsert = apiClient.PutAsync; // no RowVersion indicates insert if (model.RowVersion.IsNullOrEmpty()) { upsert = apiClient.PostAsync; } using (var response = await upsert(apiUrl, content)) { // init result var result = new ResourceResult <UploadedFileResource>(resource); // read result from RESTful service var responseDocument = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) { // Fetch created or updated resource from response result.Resource = JsonConvert.DeserializeObject <UploadedFileResource>(responseDocument);; } else { // fetch errors and or exceptions result = JsonConvert.DeserializeObject <ResourceResult <UploadedFileResource> >(responseDocument); } // Set error message for concurrency error if (response.StatusCode == HttpStatusCode.Conflict) { result.Errors.Clear(); result.Errors.Add(new ValidationError("This record is modified by another user")); result.Errors.Add(new ValidationError("Your work is not saved and replaced with new content")); result.Errors.Add(new ValidationError("Please review the new content and if required edit and save again")); } if (response.StatusCode.IsInSet(HttpStatusCode.OK, HttpStatusCode.Created, HttpStatusCode.Conflict)) { return(StatusCode(response.StatusCode.ToInt32(), result)); } // copy errors so they will be rendered in edit form foreach (var error in result.Errors) { ModelState.AddModelError(error.MemberName ?? "", error.Message); } // Update model with Beautify effect(s) and make it visible in the partial view IEnumerable <PropertyInfo> properties = model.GetType().GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { var rawValue = property.GetValue(model); var attemptedValue = rawValue == null ? "" : Convert.ToString(rawValue, CultureInfo.InvariantCulture); ModelState.SetModelValue(property.Name, rawValue, attemptedValue); } // No need to specify model here, it has no effect on the render process :-( return(PartialView()); } } }
/// <summary> /// Update attached file in database /// </summary> /// <param name="file"></param> /// <returns></returns> public int UpdateAttachedFile(UploadedFileModel file, int serviceId) { return(Repository.UpdateAttachedFile(file, serviceId)); }