amb-imagenes.component.ts
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Component, OnInit } from '@angular/core';
import { appSettings } from 'src/etc/AppSettings';
import { ProductoService } from 'src/app/services/producto.service';
import { Producto } from 'src/app/wrappers/producto';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-amb-imagenes',
templateUrl: './amb-imagenes.component.html',
styleUrls: ['./amb-imagenes.component.scss']
})
export class AmbImagenesComponent implements OnInit {
apiUrl = appSettings.apiUrl;
articulos: Producto[] = [];
private auxProductos: Producto[] = [];
private searchTerm: string = '';
constructor(private productoService: ProductoService, private http: HttpClient) {}
ngOnInit() {
this.productoService.getAll().subscribe((productos: Producto[]) => {
this.articulos = productos;
this.filterItems();
});
}
onFileSelected(event, articulo) {
let file: File = event.target.files[0];
this.onLoad(file)
.then(result => {
articulo.imagenes = [];
document.getElementById(articulo.CodSec + articulo.CodArt)['src'] = result;
this.saveInBase({
name: file.name,
base64: result,
codigo: articulo.CodArt,
sector: articulo.CodSec
});
});
}
filterItems() {
this.auxProductos = this.articulos.filter(x => {
return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase())
});
}
saveInBase(img) {
this.http.post(`${appSettings.apiUrl}/imagenes/guardar`, img)
.subscribe(data => {
console.log(data);
});
}
onLoad(file) {
return new Promise((resolve, reject) => {
var fr = new FileReader();
fr.onload = function() {
resolve(fr.result);
};
fr.readAsDataURL(file);
});
}
}