Skip to content

Commit

Permalink
Implement the client-side to upload a photo.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosh-hamedani committed Apr 26, 2017
1 parent 9cd128a commit 7c3cdda
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ClientApp/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';
import { VehicleFormComponent } from './components/vehicle-form/vehicle-form.component';
import { PhotoService } from "./services/photo.service";

Raven.config('https://[email protected]/155312').install();

Expand Down Expand Up @@ -50,7 +51,8 @@ Raven.config('https://[email protected]/155312').instal
],
providers: [
{ provide: ErrorHandler, useClass: AppErrorHandler },
VehicleService
VehicleService,
PhotoService
]
})
export class AppModule {
Expand Down
1 change: 1 addition & 0 deletions ClientApp/app/components/view-vehicle/view-vehicle.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h2>Contact</h2>
<!-- Photos tab -->
<div role="tabpanel" class="tab-pane" id="photos">
<h2>Photos</h2>
<input type="file" (change)="uploadPhoto()" #fileInput>
</div>
</div>
</div>
Expand Down
12 changes: 11 additions & 1 deletion ClientApp/app/components/view-vehicle/view-vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { PhotoService } from './../../services/photo.service';
import { ToastyService } from 'ng2-toasty';
import { VehicleService } from './../../services/vehicle.service';
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
templateUrl: 'view-vehicle.html'
})
export class ViewVehicleComponent implements OnInit {
@ViewChild('fileInput') fileInput: ElementRef;
vehicle: any;
vehicleId: number;

constructor(
private route: ActivatedRoute,
private router: Router,
private toasty: ToastyService,
private photoService: PhotoService,
private vehicleService: VehicleService) {

route.params.subscribe(p => {
Expand Down Expand Up @@ -45,4 +48,11 @@ export class ViewVehicleComponent implements OnInit {
});
}
}

uploadPhoto() {
var nativeElement: HTMLInputElement = this.fileInput.nativeElement;

this.photoService.upload(this.vehicleId, nativeElement.files[0])
.subscribe(x => console.log(x));
}
}
15 changes: 15 additions & 0 deletions ClientApp/app/services/photo.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Http } from '@angular/http';
import { Injectable } from '@angular/core';

@Injectable()
export class PhotoService {

constructor(private http: Http) { }

upload(vehicleId, photo) {
var formData = new FormData();
formData.append('file', photo);
return this.http.post(`/api/vehicles/${vehicleId}/photos`, formData)
.map(res => res.json());
}
}

0 comments on commit 7c3cdda

Please sign in to comment.