我有以下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返回的数据时,文档返回正常,但是在保存时它总是空的?
答案 0 :(得分:2)
调用API端点时,需要将responseType设置为数组缓冲区,如下所示:
var promise = $http.post('http://someurl', formData, {responseType: 'arraybuffer'});
return promise;
然后文件会正确保存。