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?
I had the same problem. Just add responseType: 'arraybuffer'
to your get configuration.
上一篇: 适用于PDF文件的MIME媒体类型