Last active
January 15, 2020 18:15
-
-
Save tinmegali/2e1b30b96bb401eeb0322c80066d7472 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- main image --> | |
<div>ImageMain: {{course.imageMain}}</div> | |
<img *ngIf="imageUrl" [src]="imageUrl"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FileService } from 'app/files/file.service'; | |
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'; | |
@Component({ | |
selector: 'entity-detail', | |
templateUrl: './entity-detail.component.html' | |
}) | |
export class EntityDetailComponent implements OnInit { | |
imageMain?: Blob; | |
imageUrl?; | |
constructor( | |
protected fileService: FileService, | |
protected sanitizer: DomSanitizer | |
) {} | |
ngOnInit() { | |
this.activatedRoute.data.subscribe(({ entity }) => { | |
this.entity = entity; | |
this.fileService.download(this.entity.imageUrl).subscribe(res => { | |
console.log('image downloaded...'); | |
this.imageMain = new Blob([res.body], { type: res.headers.get('Context-Type') }); | |
this.imageUrl = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(this.imageMain)); | |
}); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-group"> | |
<label class="form-control-label" for="imageMain">Image Main</label> | |
<input type="file" formControlName="imageMain" name="imageMain" id="imageMain" | |
(change)="onFileSelect($event)"> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FormBuilder, FormControl, FormGroup } from '@angular/forms'; | |
import { FileService } from 'app/files/file.service'; | |
import { IFileResponse } from 'app/shared/model/file-response.model'; | |
@Component({ | |
selector: 'entity-update', | |
templateUrl: './entity-update.component.html' | |
}) | |
export class EntityUpdateComponent implements OnInit { | |
imageMain?: File; | |
constructor( | |
//... | |
protected fileService: FileService, | |
protected fb: FormBuilder | |
) {} | |
setNewForm(institutionId: number) { | |
this.form = this.fb.group({ | |
imageUrl: new FormControl() | |
}); | |
} | |
onFileSelect(event) { | |
if (event.target.files.length > 0) { | |
const file = event.target.files[0]; | |
console.log('selected file: ' + file.name); | |
this.imageMain = file; | |
} | |
} | |
uploadMainImage(file: File) { | |
console.log(`uploading main image: ` + file.name); | |
this.fileService.upload(file).subscribe( | |
(response: HttpResponse<IFileResponse>) => { | |
this.course.imageUrl = response.body.fileName; | |
}, | |
(res: HttpErrorResponse) => { | |
console.error(`error saving file: ${file.name}`); | |
} | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface ICourse { | |
imageUrl?: string; | |
} | |
export class Course implements ICourse { | |
constructor( | |
public imageUrl?: string | |
) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface IFileResponse { | |
fileName?: string; | |
fileDownloadUri?: string; | |
fileType?: string; | |
size?: number; | |
} | |
export class FileResponse implements IFileResponse { | |
constructor(public fileName?: string, public fileDownloadUri?: string, public pubfileType?: string, public size?: number) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FileUtil { | |
public static blobToFile(blob: Blob, filename: string, contentType: string): File { | |
return new File([blob], filename, { type: contentType, lastModified: Date.now() }); | |
} | |
public static openFile(fileURL, fileName) { | |
const strWindowFeatures = 'menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes'; | |
const fileWindow = window.open(fileURL, fileName, strWindowFeatures); | |
setTimeout( | |
function() { | |
fileWindow.document.title = fileName; | |
}, 1000 | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient, HttpResponse } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
type EntityResponseType = HttpResponse<IFileResponse>; | |
type EntityArrayResponseType = HttpResponse<IFileResponse[]>; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class FileService { | |
public resourceUrl = SERVER_API_URL + 'api/files'; | |
constructor(protected http: HttpClient) {} | |
upload(file: File): Observable<EntityResponseType> { | |
const formData = new FormData(); | |
formData.append('file', file); | |
return this.http.post<IFileResponse>(`${this.resourceUrl}/`, formData, { observe: 'response' }); | |
} | |
download(fileName: string) { | |
return this.http.get(`${this.resourceUrl}/${fileName}`, { observe: 'response', responseType: 'blob' }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment