当前位置:网站首页>How to correctly write the binary stream of the file returned by the server to the local file and save it as a file
How to correctly write the binary stream of the file returned by the server to the local file and save it as a file
2022-07-31 11:36:00 【HUAWEI CLOUD】
我使用 Node.js 的 request 工具库,请求服务器端的视频文件,保存到本地之后,发现了问题.
我把 url 输入到浏览器里,手动下载视频文件后,文件大小为 70 多 KB:

然而使用 Node.js 代码请求文件数据并保存到本地,发现文件尺寸变成 100 多 KB 了,显然不正确:

经过研究发现,需要使用 request 在发起数据请求之前,添加如下一行语句:
request.defaults({ encoding: null });完整的数据请求的代码:
var requestC = request.defaults({ encoding: null }); console.log("get video via url: " + url ); const fileName = getVideoPartNameByUrl(url); requestC(getVideoOptions,function(error,response,body){ if(error){ console.log("error occurred: " + error); reject(error); } resolve({ fileName: fileName, fileContent: body }); }); 文件写入的代码:
fs.writeFile(oVideo.fileName, oVideo.fileContent, "binary", function (error) { if(error) console.log("file writes error"); else{ console.log("File: ", oVideo.fileName, " writes ok"); } });之后问题消失.
边栏推荐
猜你喜欢

Usage of JOIN in MySQL

Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)

瑞吉外卖项目:新增菜品与菜品分页查询

5 open source Rust web development frameworks, which one do you choose?

学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!

Many mock tools, this time I chose the right one

若枚举映射的值不存在,则不进行反序列化

SQL - Left join, Right join, Inner join

拥抱趋势!阿里这套微服务开源框架权威手册,实战到底层细致清晰

kubernetes之服务发现
随机推荐
科学论文和学术论文写作
Experience innovation and iteration through the development of lucky draw mini-programs
mpu9150(driverack pa简明教程)
关于IDEA开发工具的介绍
The principle of v-model
结构化查询语言SQL-关系数据库标准语言
Docker搭建Mysql主从复制
2022/7/28
7 天学个Go,Go 结构体 + Go range 来学学
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
LeetCode 1161.最大层内元素和:层序遍历
lotus-local-net 2k v1.17.0-rc4
apisix-Getting Started
【Web技术】1397- 深入浅出富文本编辑器
【软件工程之美 - 专栏笔记】33 | 测试工具:为什么不应该通过QQ/微信/邮件报Bug?
学自动化测试哪个培训机构好 试听课程后就选了这个地方学习
LeetCode - 025. 链表中的两数相加
Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
三六零与公安部三所发布报告:关基设施保护成为网络安全博弈关键