2

I using Angular 6.

I have simple input type="file" that pass data to img scr that show me image that i need to upload.

When i select image first time nothing happens,and when i select image second time i do see the preview image.

What i am missing why i don't see the image preview in the first time?

My html

<div class="container" class="border">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <h3>Choose File</h3>
            <form (ngSubmit)="onSubmit()">
                <span style="color:red;" *ngIf="message">{{message}}</span>
                <input #file type="file"  ngf-max-size='10' accept='image/*' name="image" (change)="preview(file)">
                <img [src]="imgURL" height="200" width="200" *ngIf="imgURL">
                <div class="form-group">
                    <button class="btn btn-primary">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>

My TypeScript

export class BottomSheetComponent implements OnInit {

  token = this.pzLogin.userLoginAccessToken;
  public imagePath;
  imgURL: any = this.pzLogin.UserLoginClaims.ImageUrl;
  public message: string;
  fileData = new FileReader();

  constructor(
    private _bottomSheetRef: MatBottomSheetRef<BottomSheetComponent>,
     private http: HttpClient, private pzLogin: PrivateZoneLoginService,
     private localStorageService: LocalStorageService) { }

 /* openLink(event: MouseEvent): void {
    this._bottomSheetRef.dismiss();
    event.preventDefault();
  }*/

  ngOnInit() {

  }

  preview(event) {

    if (event.files.length === 0) {
      return;
    }

    const mimeType = event.files[0].type;
    if (mimeType.match(/image\/*/) == null) {
      this.message = 'Only images are supported.';
      return;
    }

    const fileSize = event.files[0].size;
    if (fileSize > 200839) {
      this.message = 'Maximum upload file size 200 kb.';
      return ;
    }

    const reader  = new FileReader();
    reader.readAsDataURL(event.files[0]);

    reader.onload = () => {
    this.imgURL =  reader.result;
    this.fileData = event.files;
    };
  }

  onSubmit() {
    const formData = new FormData();
   formData.append('UploadedFile', this.fileData[0], this.fileData[0].name);
   formData.append('token', this.token);
    this.http.post('http://localhost:11111/PrivateZone/UploadUserImage', formData)
      .subscribe(res => {
        console.log('res' + res);
        this.localStorageService.setItem('UserLoginClaims', res);
      });
  }
}
Vladimir Potapov
  • 2,347
  • 7
  • 44
  • 71

3 Answers3

0

you can try like this

 preview(event) {

    if (event.files.length === 0) {
      return;
    }

    const mimeType = event.files[0].type;
    if (mimeType.match(/image\/*/) == null) {
      this.message = 'Only images are supported.';
      return;
    }

    const fileSize = event.files[0].size;
    if (fileSize > 200839) {
      this.message = 'Maximum upload file size 200 kb.';
      return ;
    }

    const reader  = new FileReader();
    reader.readAsDataURL(event.files[0]);

    const self = this; // here we gave reference of this to self 
    reader.onload = () => {
    self.imgURL =  reader.result;
    self.fileData = event.files;
    };
  }

let me know if it is working or not.

Yash Rami
  • 2,276
  • 1
  • 10
  • 16
0

Try like this

preview(event) {

    if (event.files.length === 0) {
        return;
    }

    const mimeType = event.files[0].type;
    if (mimeType.match(/image\/*/) == null) {
        this.message = 'Only images are supported.';
        return;
    }

    const fileSize = event.files[0].size;
    if (fileSize > 200839) {
        this.message = 'Maximum upload file size 200 kb.';
        return ;
    }

    const reader  = new FileReader();
    reader.readAsDataURL(event.files[0]);

    const _self = this;
    reader.onload = () => {
        _self.imgURL =  reader.result;
        _self.fileData = event.files;
    };
 }
Sourav Golui
  • 583
  • 1
  • 6
  • 13
Ronika
  • 24
  • 2
0

I have this upload Image in Bottom Sheet component, and this was a problem.

You just need to add this._bottomSheetRef.containerInstance.enter();

This will load the image

Here the change that i have made:

  this.reader.onload = () => {
      this.imgURL = this.reader.result;
       this.fileData = event.files;
       this._bottomSheetRef.containerInstance.enter();
    };
Vladimir Potapov
  • 2,347
  • 7
  • 44
  • 71