0

I'm using Codeigniter and PHPExcel to generate the .xls file. When I try to save it on server everything goes ok, but I don't want to save it on server, I want to download it when Angular 7 receive the response with the blob.

My Api with PHP is like this:

function getFile:
    $this->objPHPExcel->getProperties()->setCreator("My File")->setLastModifiedBy("My File");
            $this->objPHPExcel->getProperties()->setCreator("My File")->setTitle("");
            $this->objPHPExcel->getProperties()->setCreator("My File")->setSubject("");
            $this->objPHPExcel->getProperties()->setCreator("My File")->setDescription("");
            $this->objPHPExcel->getProperties()->setCreator("My File")->setKeywords("");

    Here I have a foreach creating the rows and cols:
    $this->objPHPExcel->setActiveSheetIndex(0);
    $this->objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
    $this->objPHPExcel->getActiveSheet()->setCellValue( $col.$linha , $val);
    $this->objPHPExcel->getActiveSheet()->getStyle( $col.$linha )->getFont()->setBold( true );
    $this->objPHPExcel->getActiveSheet()->getStyle( $col.$linha )->applyFromArray($centerStyle);

    After foreach:
    $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel5');
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="file.xls"');
    header('Cache-Control: max-age=0');

    $objWriter->save('php://output');

Obs: If I set a path to the file it is created without problems:
$objWriter->save($filePath.'test.xls');

My angular 7 service is like this:

generateExcel(body){

    const url = `${baseurl.base_url}api/generate_excel/get_file`;
    const header = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
    return this.http.post<any[]>(url, body, {headers: header});
  }

And my subscribe in Component is like this:

this.generateExcelService.generateExcel(JSON.stringify(table)).subscribe((response: any) => {

      let blob = new Blob([response], {type: 'application/vnd.ms-excel'})
      saveAs(blob, 'file.xls')
    })

The response is Blob I guess. Something like this:

enter image description here

But when the subscribe runs I got the following error in console:

Error: SyntaxError: Unexpected token � in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad

I would like to know why the file isn't been downloaded when I click on the button which makes the subscribe runs and what do I have to do to make it works.

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
Dierp
  • 321
  • 2
  • 5

1 Answers1

0

I found the solution for my problem here: Make Angular expect other response than JSON

What I did to solve my problem is to change my service to make it expect other response than JSON:

 var HTTPOptions = {
      headers: new HttpHeaders({
        'Accept': 'text/html, application/xhtml+xml, */*',
        'Content-Type': 'application/x-www-form-urlencoded'
      }),
      'responseType': 'blob' as 'json'
   }
    return this.http.post<any[]>(url, body, HTTPOptions);
Dierp
  • 321
  • 2
  • 5