| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 483 人关注过本帖
标题:[Node.js] 二进制文件经过axios请求后发生变化
只看楼主 加入收藏
caibyte
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-8-10
收藏
 问题点数:0 回复次数:1 
[Node.js] 二进制文件经过axios请求后发生变化
这是一段搭建本地服务器并发送请求的 node.js 代码:

程序代码:
const fs = require('fs')
const { Buffer } = require('node:buffer')
const axios = require('axios').default
const express = require('express')

let app = express()

// 此处输出了文件原本的二进制信息
console.log('Local file buffer: ')
console.log(fs.readFileSync('a.zip'))

app.get('/', (req, res) => {
    res.sendFile('a.zip', { root: __dirname })
})

app.listen(3000, () => {
    axios({
        method: 'get',
        url: 'http://127.0.0.1:3000/'
    }).then((res) => {
        // 此处输出请求后得到的信息,显然本应和上面的信息一致
        console.log('Buffer from the request: ')
        console.log(Buffer.from(res.data))
    })
})


其中 a.zip 是本地的一个压缩包,里面只有一个空的 a.txt 文件用于测试,见
a.zip (144 Bytes)


然而在我的电脑(系统为 Windows10)上两次输出并不相同,输出如下:

程序代码:
Local file buffer: 
<Buffer 50 4b 03 04 0a 00 00 00 00 00 6b 9f 0a 57 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 61 2e 74 78 74 50 4b 01 02 3f 00 0a 00 00 00 00 00 6b 9f 0a 
... 94 more bytes>
Buffer from the request: 
<Buffer 50 4b 03 04 0a 00 00 00 00 00 6b ef bf bd 0a 57 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 61 2e 74 78 74 50 4b 01 02 3f 00 0a 00 00 00 00 00 6b 
... 108 more bytes>


其中 9f 变成了 ef bf bd,其省略部分应该还有不同。

我猜测这是由编码不同导致,所以将请求得到的字符串由 utf8 转换成 gbk 后再输出,结果为:

程序代码:
Local file buffer: 
<Buffer 50 4b 03 04 0a 00 00 00 00 00 6b 9f 0a 57 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 61 2e 74 78 74 50 4b 01 02 3f 00 0a 00 00 00 00 00 6b 9f 0a 
... 94 more bytes>
Buffer from the request: 
<Buffer 50 4b 03 04 0a 00 00 00 00 00 6b 3f 0a 57 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 61 2e 74 78 74 50 4b 01 02 3f 00 0a 00 00 00 00 00 6b 3f 0a 
... 94 more bytes>


长度虽然相同,但是 9f 变成了 3f。

请问这种问题由何造成,能否通过修改客户端的代码(即 axios 请求部分)来正确地获取文件呢?如果必须修改服务端逻辑,又该如何修改?

[此贴子已经被作者于2023-8-11 19:42编辑过]

搜索更多相关主题的帖子: 文件 请求 输出 Node Buffer 
2023-08-10 20:18
caibyte
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-8-10
收藏
得分:0 
自己搞出来了,把 responseType 换成 arraybuffer 或者 stream(好像还可以用 Blob 等类型)就行了。

以 arraybuffer 为例:

程序代码:
const fs = require('fs')
const { Buffer } = require('node:buffer')
const axios = require('axios').default
const express = require('express')

let app = express()

// 此处输出了文件原本的二进制信息
console.log('Local file buffer: ')
const buf1 = fs.readFileSync('a.zip')
console.log(buf1)

app.get('/', (req, res) => {
    res.sendFile('a.zip', { root: __dirname })
})

app.listen(3000, async () => {
    axios({
        method: 'get',
        url: 'http://127.0.0.1:3000/',
        responseType: 'arraybuffer'
    }).then((res) => {
        // 此处输出请求后得到的信息,现在和上面的信息一致了
        console.log('Buffer from the request: ')
        const buf2 = Buffer.from(res.data)
        console.log(buf2)
        console.log((buf1, buf2) === 0 ? 'equal' : 'not equal')
    })
})


可能是 axios 在用一般的文本方式读取二进制文件的时候由于编码原因会出现差异?
2023-08-11 20:05
快速回复:[Node.js] 二进制文件经过axios请求后发生变化
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044694 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved