Use Byte[] instead of File for DTO

This commit is contained in:
2025-06-28 11:03:16 -07:00
parent c08cf9ff43
commit dbf9ad464c
5 changed files with 36 additions and 26 deletions
+1
View File
@@ -32,6 +32,7 @@ CREATE TABLE IF NOT EXISTS `ProductImage` (
`ImageID` int(11) NOT NULL AUTO_INCREMENT, `ImageID` int(11) NOT NULL AUTO_INCREMENT,
`ProductID` int(11) NOT NULL, `ProductID` int(11) NOT NULL,
`Image` MEDIUMBLOB, `Image` MEDIUMBLOB,
`Name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`ImageID`,`ProductID`) PRIMARY KEY (`ImageID`,`ProductID`)
) AUTO_INCREMENT=1; ) AUTO_INCREMENT=1;
@@ -67,17 +67,24 @@ export class NewItemComponent {
} }
onSubmit(){ onSubmit(){
// Add uploaded files to dto
const formData = new FormData();
// Append non-file fields
formData.append("Name", this.newItem.name);
formData.append("Description", this.newItem.description);
formData.append("Cost", this.newItem.cost.toString());
formData.append("Url", this.newItem.url);
// Add image fileds
if (this.selectedFiles.length > 0){ if (this.selectedFiles.length > 0){
for(let i=0; i<this.selectedFiles.length; i++){ for(let i=0; i<this.selectedFiles.length; i++){
let building = new ProductImage(); formData.append("images", this.selectedFiles[i], this.selectedFiles[i].name);
building.image = this.selectedFiles[i];
this.newItem.images.push( building );
}; };
} }
// Proccess data // Proccess data
this.http.post<boolean>( "https://mistox.com/api/product/create", this.newItem ).subscribe({ this.http.post<boolean>( "https://mistox.com/api/product/create", formData ).subscribe({
next: async (data) => { next: async (data) => {
if (data == true){ if (data == true){
this.errorMsgs = ["Product Created Successfully"]; this.errorMsgs = ["Product Created Successfully"];
@@ -57,8 +57,20 @@ namespace MistoxWebsite.Server.Controllers {
[Route( "api/product/create" )] [Route( "api/product/create" )]
[HttpPost] [HttpPost]
public async Task<ActionResult<bool>> CreateProduct( [FromBody] Product obj ) { public async Task<ActionResult<bool>> CreateProduct([FromForm] Product obj, [FromForm] List<IFormFile> images){
try { try {
List<ProductImage> building = new List<ProductImage>();
foreach (var file in images) {
using (var stream = new MemoryStream()) {
await file.CopyToAsync(stream);
var bytes = stream.ToArray();
// Convert to your image model or whatever your logic is
ProductImage img = new ProductImage { Image = bytes, Name = file.FileName };
building.Add(img);
}
}
obj.Images = building.ToArray();
await _databaseService.NewProduct(obj); await _databaseService.NewProduct(obj);
await UpdateStore(); await UpdateStore();
return true; return true;
@@ -33,7 +33,8 @@ namespace MistoxWebsite.Server.Entities {
public class ProductImage { public class ProductImage {
public int ImageID { get; set; } // PK public int ImageID { get; set; } // PK
public int ProductID { get; set; } public int ProductID { get; set; }
public FormFile? Image { get; set; } = null; public byte[] Image { get; set; } = Array.Empty<byte>();
public string Name { get; set; } = "";
} }
public class Cart { public class Cart {
@@ -26,11 +26,13 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
int _ImageID = reader.GetInt32("ImageID"); int _ImageID = reader.GetInt32("ImageID");
int _ProductID = reader.GetInt32("ProductID"); int _ProductID = reader.GetInt32("ProductID");
byte[] _Image = (byte[])reader["Image"]; byte[] _Image = (byte[])reader["Image"];
string _Name = reader.GetString("Name");
items.Add(new ProductImage() { items.Add(new ProductImage() {
ImageID = _ImageID, ImageID = _ImageID,
ProductID = _ProductID, ProductID = _ProductID,
Image = BytesToFile(_Image) Image = _Image,
Name = _Name
}); });
} }
} }
@@ -45,13 +47,14 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
if (cur.Image != null) { if (cur.Image != null) {
string command = @" string command = @"
INSERT INTO ProductImage INSERT INTO ProductImage
(ProductID, Image) (ProductID, Image, Name)
VALUES VALUES
(@ProductID, @Image); (@ProductID, @Image, @Name);
"; ";
MySqlCommand cmd = new MySqlCommand(command, connection); MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@ProductID", Item.ID); cmd.Parameters.AddWithValue("@ProductID", Item.ID);
cmd.Parameters.AddWithValue("@Image", await FileToBytes( cur.Image )); cmd.Parameters.AddWithValue("@Image", cur.Image );
cmd.Parameters.AddWithValue("@Name", cur.Name );
await cmd.ExecuteNonQueryAsync(); await cmd.ExecuteNonQueryAsync();
} }
} }
@@ -71,19 +74,5 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
await cmd.ExecuteNonQueryAsync(); await cmd.ExecuteNonQueryAsync();
} }
} }
public async Task<byte[]> FileToBytes(IFormFile file) {
using (var memoryStream = new MemoryStream()) {
await file.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
}
public FormFile BytesToFile(byte[] fileBytes) {
var stream = new MemoryStream(fileBytes);
return new FormFile(stream, 0, fileBytes.Length, "file", Guid.NewGuid().ToString()) {
Headers = new HeaderDictionary()
};
}
} }
} }