当前位置:网站首页>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"); } });之后问题消失.
边栏推荐
猜你喜欢

Redis学习笔记-3.慢查询和其他高级数据结构

Android studio connects to MySQL and completes simple login and registration functions

LeetCode 1161.最大层内元素和:层序遍历

If the value of the enum map does not exist, deserialization is not performed

MySQL百万数据优化总结 一

面试、工作中常用sql大全(建议收藏备用)

一周精彩内容分享(第14期)

Obsidian设置图床

SQL - Left join, Right join, Inner join

Docker实践经验:Docker 上部署 mysql8 主从复制
随机推荐
Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql
v-model的原理
生信周刊第38期
mpu9150(driverack pa简明教程)
3D激光SLAM:LeGO-LOAM论文解读---点云分割部分
How MySQL's allowMultiQueries flag relates to JDBC and jOOQ
结构化查询语言SQL-关系数据库标准语言
Redis - Basics
Docker build Mysql master-slave replication
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
3D激光SLAM:LeGO-LOAM论文解读---完整篇
5 open source Rust web development frameworks, which one do you choose?
AWS Amazon cloud account registration, free application for 12 months Amazon cloud server detailed tutorial
音视频基础
使用内存映射加快PyTorch数据集的读取
MySQL 的几种碎片整理方案总结(解决delete大量数据后空间不释放的问题)
关于==和equals的区别和联系,面试这么回答就可以
R 语言data.frame 中的另一行中减去一行
数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
Usage of JOIN in MySQL