source: Sources/frontend/src/app/add-material/add-material.component.ts@ 0e4e3d1

Last change on this file since 0e4e3d1 was 8423429, checked in by AngelNasev <angel.nasev@…>, 15 months ago

Add backend and frontend projects

  • Property mode set to 100644
File size: 3.1 KB
Line 
1import {Component, OnInit} from '@angular/core';
2import {ActiveUserInterface} from "../ActiveUserInterface";
3import {LoginService} from "../login.service";
4import {MaterialsService} from "../materials.service";
5import {HttpErrorResponse, HttpEvent, HttpEventType} from "@angular/common/http";
6import {saveAs} from 'file-saver';
7import {Router} from "@angular/router";
8
9@Component({
10 selector: 'app-add-material',
11 templateUrl: './add-material.component.html',
12 styleUrls: ['./add-material.component.css']
13})
14export class AddMaterialComponent implements OnInit {
15 activeUser: ActiveUserInterface | undefined;
16 filenames: string[] = [];
17 fileStatus = {status: '', requestType: '', percent: 0};
18 courseId: number;
19
20 constructor(private loginService: LoginService,
21 private materialService: MaterialsService,
22 private router: Router) {
23 }
24
25 ngOnInit(): void {
26 this.activeUser = this.loginService.activeUser;
27 if (!this.activeUser) {
28 this.router.navigate(['/'])
29 }
30 this.courseId = this.loginService.currentCourse;
31 }
32
33 // define a function to upload files
34 onUploadFiles(e): void {
35 let files = e.target.files;
36 console.log("files", files)
37 const formData = new FormData();
38 for (const file of files) {
39 formData.append('files', file, file.name);
40 }
41 this.materialService.upload(formData, this.activeUser.username, this.loginService.currentCourse).subscribe(
42 event => {
43 console.log(event);
44 this.reportProgress(event);
45 },
46 (error: HttpErrorResponse) => {
47 console.log(error);
48 }
49 );
50 }
51
52 // define a function to download files
53 onDownloadFile(filename: string): void {
54 this.materialService.download(filename).subscribe(
55 event => {
56 console.log(event);
57 this.reportProgress(event);
58 },
59 (error: HttpErrorResponse) => {
60 console.log(error);
61 }
62 );
63 }
64
65 private reportProgress(httpEvent: HttpEvent<string[] | Blob>): void {
66 switch (httpEvent.type) {
67 case HttpEventType.UploadProgress:
68 this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Uploading... ');
69 break;
70 case HttpEventType.DownloadProgress:
71 this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Downloading... ');
72 break;
73 case HttpEventType.ResponseHeader:
74 console.log('Header returned', httpEvent);
75 break;
76 case HttpEventType.Response:
77 if (httpEvent.body instanceof Array) {
78 this.fileStatus.status = 'done';
79 for (const filename of httpEvent.body) {
80 this.filenames.unshift(filename);
81 }
82 } else {
83 saveAs(new Blob([httpEvent.body!],
84 {type: `${httpEvent.headers.get('Content-Type')};charset=utf-8`}),
85 httpEvent.headers.get('File-Name'));
86 }
87 this.fileStatus.status = 'done';
88 break;
89 default:
90 console.log(httpEvent);
91 break;
92
93 }
94 }
95
96 private updateStatus(loaded: number, total: number, requestType: string): void {
97 this.fileStatus.status = 'progress';
98 this.fileStatus.requestType = requestType;
99 this.fileStatus.percent = Math.round(100 * loaded / total);
100 }
101}
Note: See TracBrowser for help on using the repository browser.