AngularJS FileSaver 生成空文件

AngularJS FileSaver producing empty file

我有以下 PHP 代码,它从 Web 服务输出文档:

    $db->where('documentReference', $post->documentID);
    $results = $db->getOne('documents');
    $filelocation = 'doc/';
    $file = $results['filename'];
    header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Length: ' . filesize($filelocation.$file)); 
    header('Content-disposition: attachment; filename="'.$file.'"');
    readfile($filelocation.$file);

而在前端..

    APIService.registerUser($scope.formData).then(function(data){
    var blob = new Blob([data.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'});
    var config = {
        data: blob,
        filename: 'test.docx'
    };
      FileSaver.saveAs(config);
    });
}

当我检查从 API 返回的数据时,文档返回正常,但在保存时它总是空的?

调用 API 端点时,您需要将 responseType 设置为数组缓冲区,如下所示:

    var promise = $http.post('http://someurl', formData, {responseType: 'arraybuffer'});
    return promise;

然后文件被正确保存。