当前位置:网站首页>如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
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"); } });之后问题消失。
边栏推荐
- oracle优化:instr做join条件很慢「建议收藏」
- [Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
- IBM SPSS Statistics 28软件安装包下载及安装教程
- 最全phpmyadmin漏洞汇总
- 第十二章 使用中的 OpenAPI 属性
- 3.网页信息解析方法:Xpath与BeautifulSoup
- Read through the interface to call the artifact RestTemplate
- SQLServer2019 installation (Windows)
- Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
- PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用
猜你喜欢

台达PLC出现通信错误或通信超时或下载时提示机种不符的解决办法总结

最全phpmyadmin漏洞汇总

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

Hospital management system database, course design, SQLserver, pure code design

Data Persistence Technology - MP

透过开发抽奖小程序,体会创新与迭代

准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解

In PLC communication error or timeout or download the prompt solution of the model

Redis - Basics

Candence学习篇(11) allegro中设置规则,布局,走线,铺铜
随机推荐
Read through the interface to call the artifact RestTemplate
R 语言data.frame 中的另一行中减去一行
瑞吉外卖项目:新增菜品与菜品分页查询
mysql 索引使用与优化
基于Multisim的函数信号发生器–方波、三角波、正弦波[通俗易懂]
Mysql环境变量的配置(详细图解)
Obsidian设置图床
mysql 自动添加创建时间、更新时间
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql
内网渗透学习(四)域横向移动——SMB和WMI服务利用
才22岁!这位'00后'博士拟任职985高校!
Distributed id solution
《JUC并发编程 - 高级篇》06 - 共享模型之不可变(不可变类的设计 | 不可变类的使用 | 享元模式)
keras自带数据集(横线生成器)
mysql automatically adds creation time and update time
Is the working process of the belt you know the story - actionreducerstore
KVM virtualization job
蓝牙协议栈开发板 STM32F1 跑蓝牙协议栈 –传统蓝牙搜索演示以及实现原理[通俗易懂]
【Web技术】1397- 深入浅出富文本编辑器