0

I want to take only one file as input whenever i choose a file i want to take new one but my current code taking more than one file.

 <input id="input-file" type="file" name="file" (change)="onFileSelected($event)" accept="application/pdf;image/jpeg;image/gif;image/tif;image/tiff;image/jpg;image/png;application/vnd.openxmlformats-officedocument.presentationml.presentation" />

typescript code:

 onFileSelected(event){
    let file = event.target.value;

}
2
  • hsow your onFileSelected function here too Commented Aug 9, 2018 at 12:54
  • 2
    input [type="file"] always returns a FileList, even if it's just one file. To retrieve the file, use fileList[0] Commented Aug 9, 2018 at 12:57

1 Answer 1

1

If you want to upload One File

Html

<input type='file' accept="application/pdf;image/jpeg;image/gif;image/tif;image/tiff;image/jpg;image/png;application/vnd.openxmlformats-officedocument.presentationml.presentation" (change)="onSelectFile($event)" >

ts

 onSelectFile(event) {
        if (event.target.files && event.target.files[0]) {                         
                    var reader = new FileReader();        
                    reader.onload = (event) => {
                      console.log(event.target.result);
                       this.urls=event.target.result; 
                    }

                    reader.readAsDataURL(event.target.files[0]);
            }
        }

Example:https://stackblitz.com/edit/angular-multi-file-upload-preview-qt5zrt

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.