public async Task <IActionResult> Post([FromForm] FIleUpload fileUpload) { byte[] fileBytes; using (var memoryStream = new MemoryStream()) { await fileUpload.File.CopyToAsync(memoryStream); fileBytes = memoryStream.ToArray(); } var imageData = new ImageData { FileName = fileUpload.File.FileName, Type = fileUpload.File.ContentType, Description = fileUpload.Description, Location = "", Image = fileBytes }; await _imagesRepository.SaveImage(imageData); return(Ok()); }
private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.splashScreenManager1.ShowWaitForm(); ApiAccessibilities acc = new ApiAccessibilities { API_KEY = this.main_form.config.ApiKey, stmas = this.current_stmas }; APIResult add_edit; if (this.form_mode == FORM_MODE.ADD) // Add mode { add_edit = APIClient.POST(this.main_form.config.ApiUrl + "Stmas/AddStmas", acc); } else // Edit mode { add_edit = APIClient.PUT(this.main_form.config.ApiUrl + "Stmas/UpdateStmas", acc); } if (add_edit.Success) { StmasVM added_item = JsonConvert.DeserializeObject <StmasVM>(add_edit.ReturnValue); if (this.img_file_path.Trim().Length > 0 && !(this.img_file_path.StartsWith("http://") || this.img_file_path.StartsWith("https://"))) // have image { var upload = FIleUpload.Upload(this.main_form.config.ApiUrl + "Stmas/UploadImage", File.OpenRead(this.img_file_path), "file1", added_item.Id.ToString() + Path.GetExtension(this.img_file_path)); if (upload.Success) { acc.stmas = added_item; acc.stmas.ProductImg = acc.stmas.Id.ToString() + Path.GetExtension(this.img_file_path); APIResult update = APIClient.PUT(this.main_form.config.ApiUrl + "Stmas/UpdateStmas", acc); if (update.Success) { APIResult create_resize_img = APIClient.GET(this.main_form.config.ApiUrl + "Stmas/CreateResizedImage", this.main_form.config.ApiKey, "&id=" + acc.stmas.Id.ToString()); if (!create_resize_img.Success) { if (this.splashScreenManager1.IsSplashFormVisible) { this.splashScreenManager1.CloseWaitForm(); } MessageBox.Show("บันทึกข้อมูลสำเร็จ, แต่การอัพโหลดรูปภาพอาจมีปัญหา กรุณาตรวจสอบรูปภาพอีกครั้งในภายหลัง", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { if (this.splashScreenManager1.IsSplashFormVisible) { this.splashScreenManager1.CloseWaitForm(); } MessageBox.Show("บันทึกข้อมูลสำเร็จ, แต่การอัพโหลดรูปภาพล้มเหลว กรุณาอัพโหลดรูปภาพอีกครั้งในภายหลัง", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { if (this.splashScreenManager1.IsSplashFormVisible) { this.splashScreenManager1.CloseWaitForm(); } MessageBox.Show("บันทึกข้อมูลสำเร็จ, แต่การอัพโหลดรูปภาพล้มเหลว กรุณาอัพโหลดรูปภาพอีกครั้งในภายหลัง", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } this.current_stmas = added_item; this.FillForm(this.current_stmas); } else // don't have image { this.current_stmas = added_item; this.FillForm(this.current_stmas); } this.form_mode = FORM_MODE.READ; this.SetFormControlState(); } else { if (this.splashScreenManager1.IsSplashFormVisible) { this.splashScreenManager1.CloseWaitForm(); } if (MessageBox.Show(add_edit.ErrorMessage, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry) { this.btnSave.PerformClick(); } else { this.current_stmas = this.backup_stmas.Clone(); this.FillForm(this.current_stmas); this.form_mode = FORM_MODE.READ; this.SetFormControlState(); } } if (this.splashScreenManager1.IsSplashFormVisible) { this.splashScreenManager1.CloseWaitForm(); } }