diff --git a/src/app/models/fileType.ts b/src/app/models/fileType.ts new file mode 100644 index 000000000..2a7fdf744 --- /dev/null +++ b/src/app/models/fileType.ts @@ -0,0 +1,11 @@ +export let FileType: +"image/apng" | +"image/bmp" | +"image/gif" | +"image/jpeg" | +"image/pjpeg" | +"image/png" | +"image/svg+xml" | +"image/tiff" | +"image/webp" | +"image/x-icon" \ No newline at end of file diff --git a/src/app/services/file/file-loader.service.ts b/src/app/services/file/file-loader.service.ts index 997c3439f..ca3a0f897 100644 --- a/src/app/services/file/file-loader.service.ts +++ b/src/app/services/file/file-loader.service.ts @@ -1,17 +1,27 @@ import { Injectable } from '@angular/core'; +import { FileType } from 'src/app/models/fileType'; + + +interface createInput { + type?: string + accept: typeof FileType[] +} @Injectable({ providedIn: 'root' }) export class FileLoaderService { + nice : typeof FileType constructor() { } - createInput({ type = 'file'}): HTMLInputElement { + createInput(param:createInput): HTMLInputElement { let input = document.createElement('input'); - input.type = type; + input.type = param.type || 'file'; + input.accept = param.accept.join(', ') + // input.onchange = () => { // // you can use this method to get file and perform respective operations @@ -24,4 +34,10 @@ export class FileLoaderService { return input } + + getFirstFile(input: HTMLInputElement) { + let files = Array.from(input.files); + return files[0] + } + } diff --git a/src/app/shared/publication/new-publication/new-publication.page.ts b/src/app/shared/publication/new-publication/new-publication.page.ts index 04c3f191b..aca4d8ac9 100644 --- a/src/app/shared/publication/new-publication/new-publication.page.ts +++ b/src/app/shared/publication/new-publication/new-publication.page.ts @@ -111,17 +111,16 @@ export class NewPublicationPage implements OnInit { } laodPicture() { - const input = this.fileLoaderService.createInput({}) + const input = this.fileLoaderService.createInput({ + accept: ['image/apng', 'image/jpeg', 'image/png'] + }) input.onchange = async () => { - // you can use this method to get file and perform respective operations - let files = Array.from(input.files); - const file1 = files[0] - - const imageData = await this.fileToBase64Service.convert(file1) + const file = this.fileLoaderService.getFirstFile(input) + const imageData = await this.fileToBase64Service.convert(file) this.capturedImage = imageData; - this.capturedImageTitle = file1.name + this.capturedImageTitle = file.name }; }