当前位置:网站首页>如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
2022-07-31 11:25:00 【华为云】
我使用 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"); } });之后问题消失。
边栏推荐
- After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again
- 矩形脉冲波形的占空比及脉冲和瞬态特征的测量
- Single sign-on principle and implementation
- 科学论文和学术论文写作
- 3D激光SLAM:LeGO-LOAM论文解读---点云分割部分
- 5 open source Rust web development frameworks, which one do you choose?
- IDEA 配置方法注释自动参数
- 分布式id解决方案
- 蓝牙协议栈开发板 STM32F1 跑蓝牙协议栈 –传统蓝牙搜索演示以及实现原理[通俗易懂]
- Docker搭建Mysql主从复制
猜你喜欢

Intranet Penetration Learning (IV) Domain Lateral Movement - SMB and WMI Service Utilization

Many mock tools, this time I chose the right one

「MySQL」- 基础增删改查

R语言做面板panelvar例子

MySQL index usage and optimization

音视频基础

Read through the interface to call the artifact RestTemplate

Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql

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

Power BI----几个常用的分析方法和相适应的视觉对象
随机推荐
2022/7/30
若枚举映射的值不存在,则不进行反序列化
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
Initial JDBC programming
Docker build Mysql master-slave replication
准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解
一文带你了解redux的工作流程——actionreducerstore
Android studio connects to MySQL and completes simple login and registration functions
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
结构化查询语言SQL-关系数据库标准语言
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
SQL - Left join, Right join, Inner join
MySQL index usage and optimization
【虚拟化生态平台】平台架构图&思路和实现细节
Many mock tools, this time I chose the right one
St. Regis Takeaway Project: New dishes and dishes paged query
便利贴--46{基于移动端长页中分页加载逻辑封装}
Detailed tutorial on distributed transaction Seata
使用内存映射加快PyTorch数据集的读取
R 语言data.frame 中的另一行中减去一行