Php 无法在AngularJS中下载文件(PPTX)

Php 无法在AngularJS中下载文件(PPTX),php,angularjs,api,downloadfile,phppresentation,Php,Angularjs,Api,Downloadfile,Phppresentation,场景 我想让一个现有的PHP脚本动态生成一个PPTX(Microsoft PowerPoint文件)(风险缓解事件摘要),可以通过API调用访问该脚本,特别是让AngularJS前端下载该文件 到目前为止,我的脚本包含一个名为generatePresentation的函数 (以下为实际名称)。它可以在浏览器中打开脚本并 使用PhpPresentation生成库生成动态生成的文件 问题如下 如何使PowerPoint顺利下载而不出现任何问题? 任何帮助都将不胜感激 现有PowerPoint下载功能

场景

我想让一个现有的PHP脚本动态生成一个PPTX(Microsoft PowerPoint文件)(风险缓解事件摘要),可以通过API调用访问该脚本,特别是让AngularJS前端下载该文件

到目前为止,我的脚本包含一个名为generatePresentation的函数 (以下为实际名称)。它可以在浏览器中打开脚本并 使用PhpPresentation生成库生成动态生成的文件

问题如下

如何使PowerPoint顺利下载而不出现任何问题?

任何帮助都将不胜感激

现有PowerPoint下载功能 示例输入(“风险事件”用于生成PowerPoint文件的元素列表)

生成的PowerPoint示例文件

AngularJS下载功能
ctrl.getRiskReport=function(){
$http.get('api/risk/'+ctrl.risk.riskid+'/report',
{响应类型:'arraybuffer'})
.然后(功能(响应){
var file=new Blob([(响应)],{type:'application/vnd.openxmlformats officedocument.presentationml.presentation'});
var downloadLink=angular.element('

API在浏览器中的示例输出(文件内容)


使用以下三个函数(前两个用于对字符串进行编码和解码)有助于成功下载文件。

链接到需要使用前两个函数的解决方案

PHP

public function generateRiskSummaryPresentation()
{
   //header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation; charset=utf-8");
   header("charset=utf8");
   //header("Content-Disposition: attachment; filename=RiskSummary.pptx");
   $oWriterPPTX = IOFactory::createWriter($this->riskPPT,'PowerPoint2007');
   return $oWriterPPTX->save('php://output');    
}
JS

function Base64Encode(str, encoding = 'utf-8') {
    var bytes = new (typeof TextEncoder === "undefined" ? TextEncoderLite : TextEncoder)(encoding).encode(str);        
    return base64js.fromByteArray(bytes);
}

function Base64Decode(str, encoding = 'utf-8') {
    var bytes = base64js.toByteArray(str);
    return new (typeof TextDecoder === "undefined" ? TextDecoderLite : TextDecoder)(encoding).decode(bytes);
}

ctrl.getRiskReport = function(){
    $http.get('api/risk/' + ctrl.risk.riskid + '/report', 
    {responseType:'arraybuffer'})
      .then(function (response) {
         var file = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', charset: 'utf-8'});
         var downloadLink = angular.element('<a style="display:none"></a>');
         downloadLink.attr('href',window.URL.createObjectURL(file));
         downloadLink.attr('target', '_blank');
         downloadLink.attr('download', 'RiskSummary.pptx');
         downloadLink[0].click();
    });
 }
函数Base64Encode(str,encoding='utf-8'){
var bytes=new(typeof textcoder==“undefined”?textcodencerLite:textcoder)(编码).encode(str);
返回base64js.fromByteArray(字节);
}
函数Base64Decode(str,编码='utf-8'){
var bytes=base64js.toByteArray(str);
返回新的(typeof TextDecoder==“undefined”?TextDecoderLite:TextDecoder)(编码)。解码(字节);
}
ctrl.getRiskReport=函数(){
$http.get('api/risk/'+ctrl.risk.riskid+'/report',
{响应类型:'arraybuffer'})
.然后(功能(响应){
var file=new Blob([response.data],{type:'application/vnd.openxmlformats officedocument.presentationml.presentation',字符集:'utf-8'});
var downloadLink=angular.element(“”);
downloadLink.attr('href',window.URL.createObjectURL(文件));
downloadLink.attr('target','u blank');
downloadLink.attr('download','RiskSummary.pptx');
下载链接[0]。单击();
});
}

是我试图实现的一个例子。单独响应不起作用,所以我在blob中使用response.data,因为响应只是一个JSON
ctrl.getRiskReport = function(){
    $http.get('api/risk/' + ctrl.risk.riskid + '/report', 
    {responseType:'arraybuffer'})
      .then(function (response) {
         var file = new Blob([(response)], {type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'});
         var downloadLink = angular.element('<a style="display:none"></a>');
         downloadLink.attr('href',window.URL.createObjectURL(file));
         downloadLink.attr('target', '_blank');
         downloadLink.attr('download', 'RiskSummary.pptx');
         downloadLink[0].click();
    });
}
public function generateRiskSummaryPresentation()
{
   //header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation; charset=utf-8");
   header("charset=utf8");
   //header("Content-Disposition: attachment; filename=RiskSummary.pptx");
   $oWriterPPTX = IOFactory::createWriter($this->riskPPT,'PowerPoint2007');
   return $oWriterPPTX->save('php://output');    
}
function Base64Encode(str, encoding = 'utf-8') {
    var bytes = new (typeof TextEncoder === "undefined" ? TextEncoderLite : TextEncoder)(encoding).encode(str);        
    return base64js.fromByteArray(bytes);
}

function Base64Decode(str, encoding = 'utf-8') {
    var bytes = base64js.toByteArray(str);
    return new (typeof TextDecoder === "undefined" ? TextDecoderLite : TextDecoder)(encoding).decode(bytes);
}

ctrl.getRiskReport = function(){
    $http.get('api/risk/' + ctrl.risk.riskid + '/report', 
    {responseType:'arraybuffer'})
      .then(function (response) {
         var file = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', charset: 'utf-8'});
         var downloadLink = angular.element('<a style="display:none"></a>');
         downloadLink.attr('href',window.URL.createObjectURL(file));
         downloadLink.attr('target', '_blank');
         downloadLink.attr('download', 'RiskSummary.pptx');
         downloadLink[0].click();
    });
 }