首页 文章

使用angularjs从PHP REST API下载docx文件

提问于
浏览
0

我有一个PHP REST API用SLIM Framework编写,我有一个api调用,它创建一个docx文件并强制下载文件 .

$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();

文件已创建,我可以通过服务器读取它 .
但问题是在客户端文件已损坏

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

有没有人有想法?

1 回答

  • 0

    我有同样的问题 . 只需将 responseType: 'arraybuffer' 添加到您的get配置中即可 .

相关问题