Ani0610 / Download-csv-file

I want to download csv file i have convert json to csv then could not able to download that csv file so kindly help me?
0 stars 0 forks source link

Solved CSV file download in excel sheet #2

Open Ani0610 opened 6 years ago

Ani0610 commented 6 years ago

I have solved this issue of Download csv file to excel sheet for both web as well as app,in this file you need to convert your json API into csv format for that we have to use Papaparse plugin ,have share the link below. http://papaparse.com/

url = encodeURI("//put your API hear");
  this.http.get(this.url).subscribe(res => {
      result = res.json();
      var csv = Papa.unparse(result);
}

after convert json to csv we need to download the csv file using cordova.file.writeFile In this we can use this method directly in component or we can use it in providers.In my case i have used in provider so i can use it in multiple components.

// File.service Provider

public save(fileDestiny, fileName, fileMimeType, fileData) {
    let blob = new Blob(<any>[fileData], { type: fileMimeType });
    this.platform.ready().then(() => {
      if (!this.platform.is('android')) {
        FileSaver.saveAs(blob, fileName);
      } else {
        this.file.writeFile(fileDestiny, fileName, blob).then(() => {
          alert("file created at: " + fileDestiny);
        }).catch((error) => {
          alert("error creating file at :" + fileDestiny);
          console.log(JSON.stringify(error, null, 2))
                console.log("An error has occurred: Code = " + error.code);
                console.log("upload error source " + error.source);
                console.log("upload error target " + error.target);
        })
      }
    })
  }

then use it in component as below.

this.fileService.save(this.file.externalRootDirectory, "file.csv", "text/csv", csv); and the code of component as below.

url = encodeURI("//put your API Url hear");
  this.http.get(this.url).subscribe(res => {
      result = res.json();
      var csv = Papa.unparse(result);
      this.fileService.save(this.file.externalRootDirectory, "file.csv", "text/csv", csv);
}

Thank you.

Hazellaura commented 6 years ago

Thank you @Ani0610