Redirecting to a new page after loading a json file does not work

43 views Asked by At

I'm working on a project with Angular. I would like to let the user load a json file that was previously saved via my site. After the data has been successfully loaded, the user should then be directed to another page.

This is the loading function:

onFileSelected(event: any): void {
    const file = event.target.files[0];
    if (file) {
      this.dataService.resetAllData()
      const fileReader = new FileReader();
      fileReader.readAsText(file, 'UTF-8');
      fileReader.onload = () => {
        try {
          const json = JSON.parse(fileReader.result as string);
          this.allData = json;
          console.log('JSON-Daten geladen:', this.allData);
          this.dataService.setAllData(this.allData);
          window.location.href = "/planung";
        } catch (e) {
          console.error('Fehler beim Parsen der JSON-Datei:', e);
        }
      };
      fileReader.onerror = (error) => {
        console.error('Fehler beim Lesen der Datei:', error);
      };
    }
  }

Without the line "window.location.href = "/planung";" it all works great. With the line the redirect works, but without loading the Data. It seems as if the data appears briefly but then disappears after being forwarded.

What mistake did I make?

0

There are 0 answers