当前位置:网站首页>如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
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"); } });
之后问题消失。
边栏推荐
- MySQL row-level locks (row locks, adjacent key locks, gap locks)
- How MySQL's allowMultiQueries flag relates to JDBC and jOOQ
- MySQL 的几种碎片整理方案总结(解决delete大量数据后空间不释放的问题)
- Redis - Basics
- Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
- Single sign-on principle and implementation
- [Go Affair] See through Go's collections and slices at a glance
- 矩形脉冲波形的占空比及脉冲和瞬态特征的测量
- 3D激光SLAM:LeGO-LOAM论文解读---完整篇
- 学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
猜你喜欢
mysql 索引使用与优化
Obsidian设置图床
The most complete phpmyadmin vulnerability summary
unity computeshader的可读写buffer
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
才22岁!这位'00后'博士拟任职985高校!
学自动化测试哪个培训机构好 试听课程后就选了这个地方学习
淀粉与纤维素
Docker practical experience: Deploy mysql8 master-slave replication on Docker
Cloudera Manager —— 端到端的企业数据中心管理工具
随机推荐
mysql 自动添加创建时间、更新时间
Many mock tools, this time I chose the right one
AWS Amazon cloud account registration, free application for 12 months Amazon cloud server detailed tutorial
突破传统可靠性测试:混沌工程优秀实践
AWS亚马逊云账号注册,免费申请12个月亚马逊云服务器详细教程
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
3D激光SLAM:LeGO-LOAM论文解读---完整篇
Curl 命令使用
矩形脉冲波形的占空比及脉冲和瞬态特征的测量
The principle of v-model
xmind使用指南(XMind具有下列哪些功能)
一文吃透接口调用神器RestTemplate
三层架构service、dao、controller层
R语言:文本(字符串)处理与正则表达式
Threading(in thread main)
5 个开源的 Rust Web 开发框架,你选择哪个?
Sql optimization summary!detailed!(Required for the latest interview in 2021)
SQL - Left join, Right join, Inner join
The most complete phpmyadmin vulnerability summary
2022/7/28