2015-10-13 49 views
1

我已经从web服务输出一个文档下面的PHP代码:AngularJS FileSaver产生空文件

$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文档返回正常,但保存时它总是空的?

回答

2

当调用API端点,需要设置的responseType数组缓冲区像这样:

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

的文件,然后才能正确保存。

+0

此外,如果您从PHP返回,请确保您在关闭后没有换行符?> –