Skip to content

Commit a3a4d53

Browse files
committed
Angular Forms Course
1 parent 82072e7 commit a3a4d53

10 files changed

+134
-2
lines changed

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@angular/router": "11.0.0",
2727
"core-js": "^2.4.1",
2828
"express": "^4.16.2",
29+
"express-fileupload": "^1.2.0",
2930
"moment": "^2.22.2",
3031
"rxjs": "6.5.4",
3132
"tslib": "^2.0.0",

server/file-upload.route.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import {Request, Response} from 'express';
3+
4+
5+
export function onFileupload(req:Request, res: Response) {
6+
7+
let file = req['files'].thumbnail;
8+
9+
console.log("File uploaded: ", file.name);
10+
11+
res.status(200).json({message: 'File uploaded successfully.'})
12+
13+
}

server/server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import {Application} from "express";
55
import {getAllCourses, getCourseById} from "./get-courses.route";
66
import {searchLessons} from "./search-lessons.route";
77
import {getCourseCategories} from './course-categories.route';
8+
import {onFileupload} from './file-upload.route';
9+
const fileUpload = require('express-fileupload');
810

911

1012
const app: Application = express();
1113

14+
app.use(fileUpload());
15+
1216

1317
app.route('/api/courses').get(getAllCourses);
1418

@@ -18,6 +22,8 @@ app.route('/api/lessons').get(searchLessons);
1822

1923
app.route('/api/course-categories').get(getCourseCategories);
2024

25+
app.route('/api/thumbnail-upload').post(onFileupload);
26+
2127

2228
const httpServer:any = app.listen(9000, () => {
2329
console.log("HTTP REST API Server running at http://localhost:" + httpServer.address().port);

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {MatStepperModule} from '@angular/material/stepper';
4141
import { AddressFormComponent } from './address-form/address-form.component';
4242
import {CreateCourseStep3Component} from './create-course/create-course-step-3/create-course-step-3.component';
4343
import {CreateCourseStep1Component} from './create-course/create-course-step-1/create-course-step-1.component';
44+
import {FileUploadComponent} from './file-upload/file-upload.component';
4445

4546
@NgModule({
4647
declarations: [
@@ -57,7 +58,8 @@ import {CreateCourseStep1Component} from './create-course/create-course-step-1/c
5758
CreateCourseStep1Component,
5859
CreateCourseStep2Component,
5960
CreateCourseStep3Component,
60-
AddressFormComponent
61+
AddressFormComponent,
62+
FileUploadComponent
6163
],
6264
imports: [
6365
BrowserModule,

src/app/create-course/create-course-step-2/create-course-step-2.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
</mat-form-field>
3333

34+
<file-upload requiredFileType="image/png"></file-upload>
35+
3436
<!--div class="form-val">
3537
3638
{{form.errors | json}}

src/app/create-course/create-course-step-2/create-course-step-2.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export class CreateCourseStep2Component implements OnInit {
1717
price: [null, [Validators.required, Validators.pattern('[0-9]+'), Validators.min(1), Validators.max(9999)]],
1818
courseType: ['premium', Validators.required],
1919
promoPeriodStartAt: [null],
20-
promoPeriodEndAt: [null]
20+
promoPeriodEndAt: [null],
21+
thumbnailFile: [null, Validators.required]
2122
}, {
2223
validators: createPromoRangeValidator()
2324
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
<input type="file" class="file-input" (change)="onFileSelected($event)" [accept]="requiredFileType" #fileUpload>
4+
5+
6+
<div class="file-upload">
7+
8+
<mat-form-field>
9+
10+
<input matInput [disabled]="true" [value]="fileName">
11+
12+
</mat-form-field>
13+
14+
<button mat-mini-fab color="primary" class="upload-btn" (click)="fileUpload.click()">
15+
<mat-icon>
16+
attach_file
17+
</mat-icon>
18+
</button>
19+
20+
</div>
21+
22+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
.file-input {
3+
display: none;
4+
}
5+
6+
.file-upload {
7+
display: flex;
8+
align-items: flex-end;
9+
}
10+
11+
.upload-btn {
12+
margin-left: 10px;
13+
margin-bottom:15px;
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {Component, Input} from '@angular/core';
2+
import {HttpClient} from '@angular/common/http';
3+
4+
5+
@Component({
6+
selector: 'file-upload',
7+
templateUrl: "file-upload.component.html",
8+
styleUrls: ["file-upload.component.scss"]
9+
})
10+
export class FileUploadComponent {
11+
12+
@Input()
13+
requiredFileType:string;
14+
15+
fileName:string = '';
16+
17+
constructor(private http: HttpClient) {
18+
19+
}
20+
21+
22+
onFileSelected(event) {
23+
24+
const file: File = event.target.files[0];
25+
26+
this.fileName = file.name;
27+
28+
const formData = new FormData();
29+
30+
formData.append('thumbnail', file);
31+
32+
this.http.post("/api/thumbnail-upload", formData, {
33+
reportProgress: true,
34+
observe: 'events'
35+
})
36+
.subscribe(console.log);
37+
38+
}
39+
40+
41+
42+
}

0 commit comments

Comments
 (0)