当前位置:网站首页>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"); } });之后问题消失.
边栏推荐
- 面试、工作中常用sql大全(建议收藏备用)
- Redis - Basics
- 7 天学个Go,Go 结构体 + Go range 来学学
- 学习笔记 Golang 写入文件(io.WriteString、ioutil.WriteFile、file.Write、write.WriteString)
- mysql automatically adds creation time and update time
- MySQL index usage and optimization
- Many mock tools, this time I chose the right one
- St. Regis Takeaway Project: File Upload and Download
- deeplab implements its own remote sensing geological segmentation dataset
- Candence学习篇(11) allegro中设置规则,布局,走线,铺铜
猜你喜欢

PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用

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

SQL - Left join, Right join, Inner join

安装MYSQL遇到问题:write configuration file卡主

MySQL 行级锁(行锁、临键锁、间隙锁)

Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案

Obsidian设置图床

淀粉与纤维素

Docker搭建Mysql主从复制

Mysql环境变量的配置(详细图解)
随机推荐
最全phpmyadmin漏洞汇总
Experience innovation and iteration through the development of lucky draw mini-programs
502 bad gateway原因、解决方法
B/S架构模式的一个整体执行流程
Is the working process of the belt you know the story - actionreducerstore
LeetCode 1161.最大层内元素和:层序遍历
科学论文和学术论文写作
MySQL index usage and optimization
MySQL limit paging query and performance issues
How MySQL's allowMultiQueries flag relates to JDBC and jOOQ
Obsidian设置图床
若枚举映射的值不存在,则不进行反序列化
应用层基础 —— 认识URL
mysql根据多字段分组——group by带两个或多个参数
Yarn安装配置(vsftpd安装配置)
mysql automatically adds creation time and update time
apisix-入门使用篇
分布式id解决方案
【软件工程之美 - 专栏笔记】33 | 测试工具:为什么不应该通过QQ/微信/邮件报Bug?
安装MYSQL遇到问题:write configuration file卡主