Skip to content

Commit

Permalink
NG Upload Image to API
Browse files Browse the repository at this point in the history
  • Loading branch information
chhinsras committed Aug 26, 2021
1 parent a1c740e commit 96e575e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,4 @@ FodyWeavers.xsd

src/server/.idea/.idea.FluentPOS/.idea/
.DS_Store
src/server/API/Files/**
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,14 @@ export class ProductFormComponent implements OnInit {

onSubmit() {
// TODO after successful update/insert, refresh table view in component product.component.ts
console.log(this.url);
// var upload = new Upload();
// upload.data = this.url;
// upload.name = this.url.

if (this.productForm.valid) {
if (this.productForm.get('id').value === '' || this.productForm.get('id').value == null) {
this.productService.createProduct(this.productForm.value).subscribe(response => {
this.productService.createProduct(this.productForm.value, this.upload).subscribe(response => {
this.toastr.success(response.messages[0]);
});
} else {
this.productService.updateProduct(this.productForm.value).subscribe(response => {
this.productService.updateProduct(this.productForm.value, this.upload).subscribe(response => {
this.toastr.success(response.messages[0]);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/src/app/modules/admin/catalog/models/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export interface Product {
isAlert?: boolean;
alertQuantity: number;
detail: string;
upload?: Upload;
uploadRequest?: Upload;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ProductApiService } from 'src/app/core/api/catalog/product-api.service';
import { Upload } from 'src/app/core/models/uploads/upload';
import { IResult } from 'src/app/core/models/wrappers/IResult';
import { PaginatedResult } from 'src/app/core/models/wrappers/PaginatedResult';
import { Result } from 'src/app/core/models/wrappers/Result';
Expand Down Expand Up @@ -38,13 +39,16 @@ export class ProductService {
return this.api.getById(id).pipe(map((response: Result<Product>) => response));
}

createProduct(product: Product): Observable<IResult<Product>> {
createProduct(product: Product, upload: Upload): Observable<IResult<Product>> {
if (upload != undefined && upload.data != undefined) product.uploadRequest = upload;
return this.api
.create(product)
.pipe(map((response: IResult<Product>) => response));
}

updateProduct(product: Product): Observable<IResult<Product>> {
updateProduct(product: Product, upload: Upload): Observable<IResult<Product>> {
console.log(upload);
if (upload != undefined && upload.data != undefined) product.uploadRequest = upload;
return this.api
.update(product)
.pipe(map((response: IResult<Product>) => response));
Expand Down
2 changes: 1 addition & 1 deletion src/server/Shared/Shared.DTOs/Upload/UploadRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class UploadRequest

public UploadType UploadType { get; set; }

public byte[] Data { get; set; }
public string Data { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
// </copyright>
// --------------------------------------------------------------------------------------------------

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using FluentPOS.Shared.Core.Interfaces.Services;
using FluentPOS.Shared.DTOs.Upload;
Expand All @@ -23,7 +25,9 @@ public Task<string> UploadAsync(UploadRequest request)
return Task.FromResult(string.Empty);
}

var streamData = new MemoryStream(request.Data);
var base64Data = Regex.Match(request.Data, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;

var streamData = new MemoryStream(Convert.FromBase64String(base64Data));
if (streamData.Length > 0)
{
string folder = request.UploadType.ToDescriptionString();
Expand Down

0 comments on commit 96e575e

Please sign in to comment.