首页 新闻 搜索 专区 学院

JavaScript 中,从后台得到的是二进制数组,这样的数组要怎么样处理,才能正常的使用

1
悬赏园豆:200 [待解决问题]
    function fileDown() {//获取文件的方法
        var tet = $scope.tet;
        FileService.downloadFile(tet.fileId, Finished);
        function Finished(err,result) {
            if (!err && result) {
                //输出结果
                var obj = result.FileInfo;
            }
            else {
                console.log(err);
            }
        }
    }

1.这是从后台获取的数据的代码

{"Status":"OK","FileInfo":{"Data":{"type":"Buffer","data":[80,75,3,4,45,0,0,0,8,0,216,93,189,68,158,126,171,237,255,255,255,255,255,255,255,255,23,0,20,0,54,51,53,51,54,57,54,48,56,48,56,51,54,52,48,52,52,50,46,104,116,109,108,1,0,16,0,138,0,0,0,0,0,0,0,127,0,0,0,0,0,0,0,179,81,212,213,85,40,78,44,75,77,81,72,43,202,207,85,40,45,202,177,213,48,0,2,77,5,93,93,59,94,46,27,143,16,95,31,59,27,15,87,71,23,59,27,125,48,5,20,116,242,119,137,84,72,206,207,43,73,205,43,113,77,201,44,73,76,202,73,181,77,75,204,41,78,5,201,6,216,237,63,187,123,249,182,211,91,62,237,95,181,117,175,161,234,153,135,167,246,157,93,113,113,219,182,211,251,111,109,94,115,106,163,141,83,16,208,172,0,32,6,153,3,50,22,100,7,0,80,75,1,2,45,0,45,0,0,0,8,0,216,93,189,68,158,126,171,237,255,255,255,255,255,255,255,255,23,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,54,51,53,51,54,57,54,48,56,48,56,51,54,52,48,52,52,50,46,104,116,109,108,1,0,16,0,138,0,0,0,0,0,0,0,127,0,0,0,0,0,0,0,80,75,5,6,0,0,0,0,1,0,1,0,89,0,0,0,200,0,0,0,0,0]},"FileType":"","FileName":"635369608083640442.zip"}}

2.这是后台得到数据

需求:

**1.怎么才能将这数据变成直接使用的数据。

欢迎各位大神,前辈帮忙!!!

搬运工小菜的主页 搬运工小菜 | 初学一级 | 园豆:2
提问于:2017-03-13 17:36
< >
分享
所有回答(5)
0

JSON.parse(str);

解析一下就成对象了,前掉它是标准的json格式。

angelshelter | 园豆:9678 (大侠五级) | 2017-03-13 17:46

这个方法不行,我要的数据是data:[这个数组],你这个是试了下,没有效果

支持(0) 反对(0) 搬运工小菜 | 园豆:2 (初学一级) | 2017-03-13 17:52
0

你的数据五六七八全有,哪里是二进制了?

逐影 | 园豆:998 (小虾三级) | 2017-03-13 17:59

{ Status:"OK", FileInfo: { Data:文件数据 FileType:文件类型 FileName:文件名 } }我只要用到文件数据里面的

支持(0) 反对(0) 搬运工小菜 | 园豆:2 (初学一级) | 2017-03-13 18:16

@茶味: 

function ab2str(buf) {
  return String.fromCharCode.apply(null, new Uint16Array(buf));
}

var buffer = [80,75,3,4,45,0,0,0,8,0,216,93,189,68,158,126,171,237,255,255,255,255,255,255,255,255,23,0,20,0,54,51,53,51,54,57,54,48,56,48,56,51,54,52,48,52,52,50,46,104,116,109,108,1,0,16,0,138,0,0,0,0,0,0,0,127,0,0,0,0,0,0,0,179,81,212,213,85,40,78,44,75,77,81,72,43,202,207,85,40,45,202,177,213,48,0,2,77,5,93,93,59,94,46,27,143,16,95,31,59,27,15,87,71,23,59,27,125,48,5,20,116,242,119,137,84,72,206,207,43,73,205,43,113,77,201,44,73,76,202,73,181,77,75,204,41,78,5,201,6,216,237,63,187,123,249,182,211,91,62,237,95,181,117,175,161,234,153,135,167,246,157,93,113,113,219,182,211,251,111,109,94,115,106,163,141,83,16,208,172,0,32,6,153,3,50,22,100,7,0,80,75,1,2,45,0,45,0,0,0,8,0,216,93,189,68,158,126,171,237,255,255,255,255,255,255,255,255,23,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,54,51,53,51,54,57,54,48,56,48,56,51,54,52,48,52,52,50,46,104,116,109,108,1,0,16,0,138,0,0,0,0,0,0,0,127,0,0,0,0,0,0,0,80,75,5,6,0,0,0,0,1,0,1,0,89,0,0,0,200,0,0,0,0,0];

console.log(ab2str(buffer));

//PK-Ø]½Dž~«íÿÿÿÿÿÿÿÿ635369608083640442.htmlŠ³QÔÕU(N,KMQH+ÊÏU(-ʱÕ0M]];^._;WG;}0tòw‰THÎÏ+IÍ+qMÉ,ILÊIµMKÌ)NÉØí?»{ù¶Ó[>í_µu¯¡ê™‡§ö]qqÛ¶Óûom^sj£SЬ ™2dPK--Ø]½Dž~«íÿÿÿÿÿÿÿÿ635369608083640442.htmlŠPKYÈ

这是解析出来的数据,但为什么是乱码就不清楚了。

支持(0) 反对(0) 逐影 | 园豆:998 (小虾三级) | 2017-03-13 19:57

@逐影: 我现在也是这个问题,我现在也是乱码。

    function fileDown() {//获取文件的方法
        FileService.downloadFile("17011288",function(err,result){
            if(err||!result){
                alert(err);
                return;
            }
            //var filedata = new Blob(result.FileInfo.Data.data);
            var jszip = new JSZip();
            jszip.loadAsync(result.FileInfo.Data.data)
                .then(function(zip){
                    zip.forEach(function(fileName){
                        var file = zip.file(fileName);
                        if(!file.dir){
                            file.async("string")
                                .then(function(content){
                                    console.log(content);//这里
                                });
                        }
                    })
                },
                function(err){
                    console.error(err)
                    return;
                });
        })
    }
支持(0) 反对(0) 搬运工小菜 | 园豆:2 (初学一级) | 2017-03-13 19:59

@茶味: 也许应该和后端人员沟通下,问问他们这些数据是怎么处理的,文件是否有损坏。

支持(0) 反对(0) 逐影 | 园豆:998 (小虾三级) | 2017-03-13 20:08
0

问题是你要怎么用?

吴瑞祥 | 园豆:28979 (高人七级) | 2017-03-13 18:06

里面应该是HTML页面||图片,我就是想解出来,然后直接{{xxx}}或者是href=“xxx”,SRC="xxx"这样的

支持(0) 反对(0) 搬运工小菜 | 园豆:2 (初学一级) | 2017-03-13 18:18

@茶味: 他filename里不是都有zip了.

如果你要用至少要先用zip解出来.至于你说的图片.那就直接拿到二进制转到base64就能给img标签用了.

这种用法确实是能用

支持(0) 反对(0) 吴瑞祥 | 园豆:28979 (高人七级) | 2017-03-13 18:23

@吴瑞祥: 我现在都没取到zip 的文件,就是得到了这个二进制的数据,zip的我是用了zip.js来解压了,但是我没源文件啊

支持(0) 反对(0) 搬运工小菜 | 园豆:2 (初学一级) | 2017-03-13 18:25

@茶味: 他那个数组不就是文件..

支持(0) 反对(0) 吴瑞祥 | 园豆:28979 (高人七级) | 2017-03-13 19:09
0
var obj={"Status":"OK","FileInfo":{"Data":{"type":"Buffer","data":[80,75,3,4,45,0,0,0,8,0,216,93,189,68,158,126,171,237,255,255,255,255,255,255,255,255,23,0,20,0,54,51,53,51,54,57,54,48,56,48,56,51,54,52,48,52,52,50,46,104,116,109,108,1,0,16,0,138,0,0,0,0,0,0,0,127,0,0,0,0,0,0,0,179,81,212,213,85,40,78,44,75,77,81,72,43,202,207,85,40,45,202,177,213,48,0,2,77,5,93,93,59,94,46,27,143,16,95,31,59,27,15,87,71,23,59,27,125,48,5,20,116,242,119,137,84,72,206,207,43,73,205,43,113,77,201,44,73,76,202,73,181,77,75,204,41,78,5,201,6,216,237,63,187,123,249,182,211,91,62,237,95,181,117,175,161,234,153,135,167,246,157,93,113,113,219,182,211,251,111,109,94,115,106,163,141,83,16,208,172,0,32,6,153,3,50,22,100,7,0,80,75,1,2,45,0,45,0,0,0,8,0,216,93,189,68,158,126,171,237,255,255,255,255,255,255,255,255,23,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,54,51,53,51,54,57,54,48,56,48,56,51,54,52,48,52,52,50,46,104,116,109,108,1,0,16,0,138,0,0,0,0,0,0,0,127,0,0,0,0,0,0,0,80,75,5,6,0,0,0,0,1,0,1,0,89,0,0,0,200,0,0,0,0,0]},"FileType":"","FileName":"635369608083640442.zip"}}
    var arr=obj.FileInfo.Data.data;
    console.log(arr);
    console.log(arr[0]);

默然沉醉 | 园豆:352 (菜鸟二级) | 2017-03-14 11:29

谢谢,但是这个不是我要的那种效果,我现在已经得到了数据,但是有新问题,就是乱码的问题

支持(0) 反对(0) 搬运工小菜 | 园豆:2 (初学一级) | 2017-03-14 11:31

@茶味: 你这个也不是二进制呀,只能按ASCII码解析,只有0-127是有效字符,128-255是扩展字符,一般不用,看着像一堆乱码,你知道这段数据大概返回的是什么信息吗?

支持(0) 反对(0) 默然沉醉 | 园豆:352 (菜鸟二级) | 2017-03-14 11:59

@默然沉醉: 一个HTML页面

支持(0) 反对(0) 搬运工小菜 | 园豆:2 (初学一级) | 2017-03-15 11:08
0
request(url, function(error, response) {})
.pipe(fs.createWriteStream(file));

你可以试试node.js

好来污影后 | 园豆:287 (菜鸟二级) | 2017-04-20 18:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册