Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to show existing image with url #40

Open
aminmohammadi05 opened this issue Jun 23, 2020 · 0 comments
Open

Unable to show existing image with url #40

aminmohammadi05 opened this issue Jun 23, 2020 · 0 comments

Comments

@aminmohammadi05
Copy link

aminmohammadi05 commented Jun 23, 2020

Here is my code:

pageImageList: PageImages[] = [];
  pageImagesForm: FormGroup;
  constructor(public authService: AuthService,
              private formBuilder: FormBuilder,
              public initializationService: InitializationService,
              private cdr: ChangeDetectorRef) {

               }

  ngOnInit() {
    this.pageImagesForm = this.formBuilder.group({
      mainPagePicture: ['', [FileValidatorDirective.validate]],
      productPagePicture: ['', [FileValidatorDirective.validate]],
      projectPagePicture: ['', [FileValidatorDirective.validate]],
      brandPagePicture: ['', [FileValidatorDirective.validate]],
      blogPagePicture: ['', [FileValidatorDirective.validate]]
    });
  }
  ngAfterViewInit() {
    this.pageImagesForm.get('mainPagePicture')
            .setValue(this.tabChanged(0));
            this.cdr.detectChanges();
  }

  onTabChanged(event) {
          if (event.index === 0) {
            this.pageImagesForm.get('mainPagePicture')
            .setValue(this.tabChanged(event.index + 1));
          } else if (event.index === 1) {
            this.pageImagesForm.get('productPagePicture')
            .setValue(this.tabChanged(event.index + 1));
          } else if (event.index === 2) {
            this.pageImagesForm.get('projectPagePicture')
            .setValue(this.tabChanged(event.index + 1));
          } else if (event.index === 3) {
            this.pageImagesForm.get('brandPagePicture')
            .setValue(this.tabChanged(event.index + 1));
          } else if (event.index === 4) {
            this.pageImagesForm.get('mainPagePicture')
            .setValue(this.tabChanged(event.index + 1));
          }
  }
tabChanged(index): InputFile[] {
    const retrievedImages = new Array<InputFile>();
    this.initializationService.getPageImages(index + 1).subscribe(images => {
        images.map( (x, i) => {
        this.toDataURL(`assets/pages/${x.pageId}_${x.imageId}.${x.fileExtension.split('/')[1]}`, (dataUrl) => {
          const inputFile: InputFile = { id: i + 1,
            preview: dataUrl,
            file: new File([dataUrl], `${x.pageId}_${x.imageId}`, {
            type: dataUrl.split(';')[0].split(':')[1]
          })};
          retrievedImages.push(inputFile);
          });
      });
      this.pageImageList = this.pageImageList.filter(y => y.pageId !== index + 1);
      this.pageImageList = [...images];
      });
      return retrievedImages;
  }

  toDataURL(url, callback) {
    const xhr = new XMLHttpRequest();
    xhr.onload = () => {
        const reader = new FileReader();
        reader.onloadend = () => {
            callback(reader.result);
        };
        reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';
    xhr.send();
}

when I retrieve the images from the urls everything is OK but when I select one of the images to change, the other image is gone!!!!
image

is there any approach to show and change existing files or images?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant