当前位置:网站首页>如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
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"); } });之后问题消失。
边栏推荐
- Redis缓存面临的缓存穿透问题
- [Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
- Android studio connects to MySQL and completes simple login and registration functions
- The latest MySql installation teaching, very detailed
- Unix知识:shell详细解读
- pycharm汉化教程(碧蓝幻想汉化插件安装)
- deeplab implements its own remote sensing geological segmentation dataset
- 学自动化测试哪个培训机构好 试听课程后就选了这个地方学习
- deeplab实现自己遥感地质分割数据集
- 新人学习小熊派华为iot介绍
猜你喜欢
随机推荐
Power BI----几个常用的分析方法和相适应的视觉对象
【虚拟化生态平台】树莓派安装虚拟化平台操作流程
Initial JDBC programming
【虚拟化生态平台】平台架构图&思路和实现细节
若枚举映射的值不存在,则不进行反序列化
MySQL row-level locks (row locks, adjacent key locks, gap locks)
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
Redis缓存面临的缓存击穿问题
【Go事】一眼看穿 Go 的集合和切片
Is the working process of the belt you know the story - actionreducerstore
安装MYSQL遇到问题:write configuration file卡主
redis-企业级使用
一、excel转pdf格式jacob.jar
Inversion problem - key point
unity computeshader的可读写buffer
Detailed tutorial on distributed transaction Seata
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
file contains vulnerabilities
mysql 索引使用与优化
The most complete phpmyadmin vulnerability summary









