当前位置:网站首页>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"); } });
之后问题消失.
边栏推荐
- mpu9150(driverack pa简明教程)
- xmind使用指南(XMind具有下列哪些功能)
- 安装MYSQL遇到问题:write configuration file卡主
- 分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
- 【软件工程之美 - 专栏笔记】33 | 测试工具:为什么不应该通过QQ/微信/邮件报Bug?
- PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用
- 无法将“node.exe”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
- 7 days to learn Go, Go structure + Go range to learn
- MySql模糊查询大全
- After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again
猜你喜欢
随机推荐
MySQL百万数据优化总结 一
淀粉与纤维素
R语言:文本(字符串)处理与正则表达式
3.网页信息解析方法:Xpath与BeautifulSoup
oracle优化:instr做join条件很慢「建议收藏」
Docker practical experience: Deploy mysql8 master-slave replication on Docker
【Go事】一眼看穿 Go 的集合和切片
mysql 索引使用与优化
Acwing-考研机试题
音视频基础
Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
安装MYSQL遇到问题:write configuration file卡主
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
Docker build Mysql master-slave replication
Cloudera Manager —— 端到端的企业数据中心管理工具
The latest MySql installation teaching, very detailed
Intranet Penetration Learning (IV) Domain Lateral Movement - SMB and WMI Service Utilization
5 open source Rust web development frameworks, which one do you choose?
7 天学个Go,Go 结构体 + Go range 来学学