Add image functionalitly to frontend

This commit is contained in:
2025-06-28 10:09:20 -07:00
parent 0323215a70
commit 01146661c7
4 changed files with 47 additions and 11 deletions
@@ -3,7 +3,13 @@ export class Product {
public name: string = "";
public description: string = "";
public curShowingIMG: number = 0;
public images: File[] = [];
public images: ProductImage[] = [];
public cost: number = 0;
public url: string = "";
}
export class ProductImage {
imageID: number = 0;
productID: number = 0;
image: File | null = null;
}
@@ -24,7 +24,7 @@
<div class="frame-item">
<!-- Need to fix for image file upload -->
<div id="FileUploadPlaceholder" ></div>
<input type="file" (change)="onFileSelected($event)" />
<input type="file" (change)="onFileSelected($event)" accept="image/*" />
</div>
<div class="flex-row">
@@ -37,3 +37,10 @@
<li *ngFor="let msg of errorMsgs" >{{ msg }}</li>
</ul>
</form>
<!-- Finish file preview for uploads -->
<div class="center big-frame background-border">
<div *ngFor="let cur of imagePreviews" id="imageHolder">
<img [src]="cur" alt="Image Preview" style="max-width: 200px;"/>
</div>
</div>
@@ -5,7 +5,7 @@ import { Router, ActivatedRoute } from '@angular/router';
import { Title } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { Authentication } from '../../../../services/Authentication';
import { Product } from 'app/models/Product';
import { Product, ProductImage } from 'app/models/Product';
@Component({
selector: 'item-new',
@@ -15,8 +15,9 @@ import { Product } from 'app/models/Product';
})
export class NewItemComponent {
newItem: Product = new Product();
readonly maxFileMB = 16;
newItem: Product = new Product();
errorMsgs: string[] = [];
constructor( private http: HttpClient, private router: Router, private route: ActivatedRoute, private title: Title, public auth: Authentication ) {
@@ -28,15 +29,39 @@ export class NewItemComponent {
}
selectedFiles: File[] = [];
onFileSelected(event: any){
this.selectedFiles = event.target.files;
imagePreviews: string[] = [];
onFileSelected(event: Event){
const fileInput = event.target as HTMLInputElement;
this.imagePreviews = [];
if (!fileInput.files?.length){
return;
}
for (let i=0; i<fileInput.files.length; i++){
let file = fileInput.files[i];
if (file.size > this.maxFileMB * 1024 * 1024){
this.errorMsgs.push("File exceeds max file size of 16MB");
continue;
}
// No issues add file to the list
this.selectedFiles.push( file );
const reader = new FileReader();
reader.onload= () => {
this.imagePreviews.push(reader.result as string);
}
reader.readAsDataURL(file);
}
}
onSubmit(){
// Add uploaded files to dto
if (this.selectedFiles.length > 0){
for(let i=0; i<this.selectedFiles.length; i++){
this.newItem.images.push( this.selectedFiles[i] );
let building = new ProductImage();
building.image = this.selectedFiles[i];
this.newItem.images.push( building );
};
}
@@ -85,7 +85,5 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
Headers = new HeaderDictionary()
};
}
}
}