AngularJS FileSaver生产空文件 [英] AngularJS FileSaver producing empty file

查看:129
本文介绍了AngularJS FileSaver生产空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PHP code从Web服务输出的文档:

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

和在前端..

    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返回的文件得到恢复正常,但在保存时,它总是空的?数据

When I inspect the data returned from the API the document get returned fine, but when being saved it's always empty?

推荐答案

在呼唤你需要设置的responseType数组缓冲区像这样的API端点:

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.

这篇关于AngularJS FileSaver生产空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆