Amazoom
Amazoom

Reputation: 661

download docx file from PHP REST API with angularjs

I have a PHP REST API has written with SLIM Framework, I have an api call which creates a docx file and force the file to be downloaded.

    $filename = 'invitation.docx';
    $templateProcessor->save($filename);
    header('Content-Description: File Transfer');
    header('Content-type: application/force-download');
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($filename));
    ob_clean();
    ob_flush();
    readfile($filename);
    exit(); 
the file is created and i can read it via the server.
but the problem is in the client side the file is corrupted

  Data.get('downloadInvitation/'+ id).then(function(results) {
        var file = new Blob([results], { type:    'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
       saveAs(file, 'invitation.docx');
       });

does anyone has an idea?

Upvotes: 0

Views: 1727

Answers (1)

MarkP
MarkP

Reputation: 171

I had the same problem. Just add responseType: 'arraybuffer' to your get configuration.

Upvotes: 0

Related Questions