public async Task <object> CreateProduct( [FromForm] TempForm form, [FromServices] CreateProduct createProduct, [FromServices] S3Client s3Client) { var product = new Product { Name = form.Name, Description = form.Description, }; var results = await Task.WhenAll(UploadFiles()); product.Images.AddRange(results.Select((path, index) => new Image { Index = index, Path = path, })); return(await createProduct.Do(product)); IEnumerable <Task <string> > UploadFiles() { var index = 0; foreach (var image in form.Images) { var fileName = $"{DateTime.Now.Ticks}_{index++}{Path.GetExtension(image.FileName)}"; yield return(s3Client.SavePublicFile($"images/{fileName}", image.OpenReadStream())); } } }
//加载临时界面 private void LoadTempForm(SplitContainer sc) { if (tmpForm != null) { tmpForm.Close(); } TempForm cForm = new TempForm(CJTopClassID, OldCJTopClassID); cForm.TopLevel = false; cForm.FormBorderStyle = FormBorderStyle.None; cForm.Dock = DockStyle.Fill; splitContainer1.Panel2.Controls.Clear(); splitContainer1.Panel2.Controls.Add(cForm); tmpForm = cForm; cForm.Show(); }
public async Task <object> CreateProduct( [FromForm] TempForm form, [FromServices] CreateProduct createProduct) { var product = new Product { Name = form.Name, Description = form.Description, }; await foreach (var image in SaveImages(form.Images)) { product.Images.Add(image); } return(await createProduct.Do(product)); }
public string UpdateProduct() { bool addSuccess = false; try { string serialize = HttpContext.Session.GetString("tempForm"); TempForm tempForm = System.Text.Json.JsonSerializer.Deserialize <TempForm>(serialize); connection.Open(); #region SqlCommand command = new SqlCommand("sp_add_Product", connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@productId", SqlDbType.Int); command.Parameters.Add("@productName", SqlDbType.NVarChar); command.Parameters.Add("@quantity", SqlDbType.Int); command.Parameters.Add("@price", SqlDbType.Float); command.Parameters.Add("@categoryId", SqlDbType.NVarChar); if (tempForm.ImageLocation != null) { command.Parameters.Add("@image", SqlDbType.NVarChar); command.Parameters["@image"].Value = tempForm.ImageLocation; } command.Parameters["@productId"].Value = tempForm.ProductId; command.Parameters["@productName"].Value = tempForm.ProductName; command.Parameters["@quantity"].Value = tempForm.Quantity; command.Parameters["@price"].Value = tempForm.Price; command.Parameters["@categoryId"].Value = tempForm.CategoryId; int row = command.ExecuteNonQuery(); if (row > 0) { addSuccess = true; } #endregion connection.Close(); } catch (Exception) { throw; } return(JsonConvert.SerializeObject(addSuccess.ToString())); }
public async Task <IActionResult> CreateProduct( [FromForm] TempForm form, [FromServices] CreateProduct createProduct, [FromServices] S3Client s3Client) { var product = new CreateProduct.Request { Name = form.Name, Description = form.Description, Value = form.Value }; var results = await Task.WhenAll(UploadFiles(s3Client, form.Images)); product.Images.AddRange(results.Select((path, index) => new Image { Index = index, Path = path, })); return(Ok(await createProduct.Do(product))); }
public static void Show(int seconds, string text) { TempForm tf = new TempForm(seconds, text); tf.Show(); }
public async Task <ActionResult> UploadAsync([FromForm] FormProduct formData) { string output = ""; LoadFirebase(); try { bool checkFile = true; if ("Create".Equals(formData.Button)) { if (formData.File == null) { return(new UnsupportedMediaTypeResult()); } } else if ("Update".Equals(formData.Button)) { if (formData.File == null) { checkFile = false; } } PushResponse response = null; if (checkFile) { string filename = formData.File.FileName; #region algorithm to check file exists. if true, rename file if (System.IO.File.Exists(fileUploadPath + filename)) { while (System.IO.File.Exists(fileUploadPath + filename)) { int lastIndex = filename.LastIndexOf("."); string input = filename.Substring(lastIndex - 3, 3); if (Regex.IsMatch(input, "^\\(\\d+\\)$")) { string number = filename.Substring(lastIndex - 2, 1); int num = Int32.Parse(number); num++; filename = filename.Replace("(" + number + ")", "(" + num + ")"); } else { filename = filename.Insert(lastIndex, "(1)"); } } string oldPath = Path.Combine(fileUploadPath, formData.File.FileName); string newPath = Path.Combine(fileUploadPath, filename); System.IO.File.Move(oldPath, newPath); } #endregion #region save file to path project var fileExtension = MimeTypeMap.GetExtension(formData.File.ContentType); using var stream = System.IO.File.Create(Path.Combine(fileUploadPath, formData.File.FileName)); formData.File.CopyToAsync(stream).Wait(); stream.Close(); #endregion #region upload file on Firebase - Realtime Database string base64 = GetBase64StringForImage(GetFilePath(filename, fileUploadPath)); output = base64; var data = new ImageModel { Img = base64 }; response = await client.PushTaskAsync("Image/", data); ImageModel result = response.ResultAs <ImageModel>(); #endregion } TempForm tempForm = new TempForm(); if ("Create".Equals(formData.Button)) { tempForm.ImageLocation = response.Result.Name; } else if ("Update".Equals(formData.Button)) { tempForm.ProductId = formData.ProductId; if (formData.File != null) { tempForm.ImageLocation = response.Result.Name; } } tempForm.ProductName = formData.ProductName; tempForm.Quantity = formData.Quantity; tempForm.Price = formData.Price; tempForm.CategoryId = formData.CategoryId; string serialize = System.Text.Json.JsonSerializer.Serialize <TempForm>(tempForm); HttpContext.Session.SetString("tempForm", serialize); return(Redirect("Product/addProduct")); } catch (Exception) { throw; } return(BadRequest()); }