当前位置:网站首页>如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
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高级篇》四、索引的存储结构

一、excel转pdf格式jacob.jar

The latest MySql installation teaching, very detailed

Is the working process of the belt you know the story - actionreducerstore

cesium-Web网页优化进阶

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

Android studio连接MySQL并完成简单的登录注册功能

才22岁!这位'00后'博士拟任职985高校!
![AtCoder—E - Σ[k=0..10^100]floor(X/10^k](/img/be/82cfab00950c1f28d426e76a792906.png)
AtCoder—E - Σ[k=0..10^100]floor(X/10^k

Redis缓存面临的缓存穿透问题
随机推荐
If the value of the enum map does not exist, deserialization is not performed
透过开发抽奖小程序,体会创新与迭代
生命不息,刷题不止,简单题学习知识点
《云原生的本手、妙手和俗手》——2022全国新高考I卷作文
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
In PLC communication error or timeout or download the prompt solution of the model
WSL2安装.NET 6
[Go Affair] See through Go's collections and slices at a glance
SQL study notes - REGEXP operator
【Go事】一眼看穿 Go 的集合和切片
Can I find a Go job in 7 days?Learn Go with arrays and pointers
Life is endless, there are more questions, simple questions to learn knowledge points
Threading(in thread main)
2022/7/30
若枚举映射的值不存在,则不进行反序列化
St. Regis Takeaway Project: File Upload and Download
The most complete phpmyadmin vulnerability summary
Initial JDBC programming
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性