public JsonResult Insert(InformationContentCRUDModel informationContentCRUDModel) { Response response = new Response(); try { informationContentProvider = new InformationContentProvider(); AppUserModel appUserModel = SessionExtension.GetSessionUser(HttpContext.Session); InformationApiContentCRUDModel apiContentCRUDModel = new InformationApiContentCRUDModel() { CategoryId = informationContentCRUDModel.CategoryId, Explanation = informationContentCRUDModel.Explanation, Title = informationContentCRUDModel.Title, LikeCount = 0, AuthorId = appUserModel.AppUser.AppUserId, TokenKey = appUserModel.TokenKey }; if (informationContentCRUDModel.InformationImage != null) { byte[] data; using (var ms = new MemoryStream()) { informationContentCRUDModel.InformationImage.CopyTo(ms); apiContentCRUDModel.PostImagePath = Path.GetExtension(informationContentCRUDModel.InformationImage.FileName); data = ms.ToArray(); } var fileContent = new ByteArrayContent(data); apiContentCRUDModel.ImageArrayList = fileContent; informationContentProvider.InsertInformationContent(apiContentCRUDModel); } response = new Response() { Message = "success", Status = true, }; } catch (Exception ex) { response = new Response() { Message = "Failed", Status = false }; } return(Json(response)); }
public InfrastructureModel InsertInformationContent(InformationApiContentCRUDModel postModel) { InfrastructureModel resultModel = new InfrastructureModel(); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", postModel.TokenKey); client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data charset=utf-8"); client.DefaultRequestHeaders.Accept.Clear(); var serializeJsonObject = JsonConvert.SerializeObject(postModel); //StringContent content = new StringContent(serializeJsonObject, Encoding.UTF8, "multipart/form-data"); MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent(); multipartFormDataContent.Add(new StringContent(postModel.Title, UTF8Encoding.UTF8), "Title"); multipartFormDataContent.Add(new StringContent(postModel.Explanation, UTF8Encoding.UTF8), "Explanation"); if (postModel.CategoryId != null) { multipartFormDataContent.Add(new StringContent(postModel.CategoryId.ToString(), UTF8Encoding.UTF8), "CategoryId"); } if (postModel.AuthorId != null) { multipartFormDataContent.Add(new StringContent(postModel.AuthorId.ToString(), UTF8Encoding.UTF8), "AuthorId"); } multipartFormDataContent.Add(postModel.ImageArrayList, "PostImageFile", Guid.NewGuid().ToString() + postModel.PostImagePath); HttpResponseMessage httpResponceMessage = client.PostAsync(ConnectionHelper.GetConnectionUrl() + "Information/InsertInformationContent/", multipartFormDataContent).Result; httpResponceMessage.EnsureSuccessStatusCode(); string response = httpResponceMessage.Content.ReadAsStringAsync().Result; return(resultModel); } }
public JsonResult InsertInformationContent([FromForm] InformationApiContentCRUDModel model) { InfrastructureModel response; try { if (model != null) { InformationContent informationContent = new InformationContent() { CategoryId = model.CategoryId, AuthorId = model.AuthorId, CreateDate = DateTime.Now, IsActive = true, IsDeleted = false, Explanation = model.Explanation, LikeCount = model.LikeCount, Title = model.Title, }; long size = model.PostImageFile.Length; if (model.PostImageFile != null) { string filePath = Path.Combine(_env.WebRootPath, "Content", "Information"); string imagePath = string.Empty; string fileExtension = Path.GetExtension(model.PostImageFile.FileName); string fileName = (Guid.NewGuid() + fileExtension); if (model.PostImageFile.Length > 0) { using (var ms = new MemoryStream()) { model.PostImageFile.CopyTo(ms); var fileBytes = ms.ToArray(); imagePath = Convert.ToBase64String(fileBytes); } var bytes = Convert.FromBase64String(imagePath); using (var imageFile = new FileStream(Path.Combine(filePath, fileName), FileMode.Create)) { imageFile.Write(bytes, 0, bytes.Length); imageFile.Flush(); } } informationContent.PostImagePath = fileName; } informationContentOperation.Insert(informationContent); response = new InfrastructureModel() { ResultStatus = true }; } else { response = new InfrastructureModel() { ResultStatus = false }; } } catch (Exception ex) { throw; } return(Json(response)); }