AngularJS FileSaver producing empty file

ε祈祈猫儿з 提交于 2019-12-11 09:54:26

问题


I have the following PHP code which 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 end..

    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 inspect the data returned from the API the document get returned fine, but when being saved it's always empty?


回答1:


When calling the API endpoint you need to set the responseType to array buffer like so:

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

The file then gets saved properly.



来源:https://stackoverflow.com/questions/33100281/angularjs-filesaver-producing-empty-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!