AngularJS FileSaver producing empty file

I have the following PHP code that outputs a document from a web service:

$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); 

And on the front side.

  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); }); } 

When I check the data returned from the API, the document is returned in order, but is it always empty when saved?

+1
source share
1 answer

When calling the API endpoint, you need to set responseType to the array buffer as follows:

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

After that, the file will be saved correctly.

+2
source

Source: https://habr.com/ru/post/977756/


All Articles