2015-10-13 51 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返回,請確保您在關閉後沒有換行符?> –