使用AngularJS从API下载word docx

时间:2016-01-30 14:55:08

标签: php angularjs

我正在尝试使用AngularJS从我的PHP Api下载Word docx文档,但是每当我尝试这样做时,我最终都会遇到Word无法读取的损坏文件。

这是我的角度代码:

  $http({
        method: 'POST',
        url: 'gen/gen_dup.php',
        data: data,
        transformRequest: $httpParamSerializer,
        responseType: 'arraybuffer',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).then(function successCallback(response) {
        console.log('Duplicata MB OK', response);
        var file = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
        saveAs(file, 'filename.docx');
    }, function errorCallback(response) {
        console.log('error', response);
    });
};

我的PHP页面代码(我使用TinyButStrong生成文档):

<?php
  include_once('tbs_class.php');
  include_once('tbs_plugin_opentbs.php');
  $TBS = new clsTinyButStrong;
  $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

$type = $_POST["type"];

if ($type == "1") {
     $TBS->LoadTemplate('templates/duplicata.docx');
} else {
     $TBS->LoadTemplate('templates/scompte.docx');
}

  $title = $_POST["title"];
  $name = $_POST["name"];
  $address = $_POST["address"];
  $reference = $_POST["reference"];
  $tmp = $_POST["collector"];
  $collector = $collectors[$tmp];
  $mcollector = $mcollectors[$tmp];

  $TBS->Show(OPENTBS_DOWNLOAD);

  ?> 

这是我从PHP收到的:

enter image description here

哪个是我的docx文件,但是当我尝试下载时,它已损坏。难道我做错了什么 ?顺便说一句,当我通过常规HTML表单将数据发送到PHP页面(并因此转到此页面)时,下载会自动开始,文件也很完美。有什么建议吗?感谢。

1 个答案:

答案 0 :(得分:0)

如果可以帮助其他人,我想出来了,问题是我的PHP文件中的一些代码产生了一些警告,而那些警告以某种方式破坏了我正在下载的文件。除了Word文档数据之外的其他任何东西都解决了这个问题。

相关问题