node.js - fs Nodejs从远程url读取数据_捕获流
问题描述
是否可以从远程 url 捕获数据并使用fs存储为文件。问题是如何收听 url?
像这样的东西:
let data = "capture from url" /// problem
let file = fs.createWriteStream(`public/capture/file.mp4`, { flags: 'a' });
file.write(data)
解决方案
试试这个代码,为此,你需要安装额外的包,比如 got、download、file-type。
const download=require("download")
const FileType=require("file-type")
const got = require('got');
async function upload(){
const url = "your URL"
const stream = got.stream(url);
let ext = await FileType.fromStream(stream);
console.log("ext", ext)
fs.writeFileSync(location to save file/filename+"."+${ext},
await download(URL));
}
upload()
推荐阅读
- reactjs - 为什么我的水平 ScrollView 不起作用?
- javascript - 我们是否需要使用 Expo 项目运行 pod install
- c - 如何读取 gcc -v 的输出以手动重新生成命令
- r - 如何阻止 RStudio 在每次启动时在“/home”目录中创建空的“R”文件夹
- python - python3 如果没有用户输入更新变量,我如何继续循环?
- javascript - 如何将 popup.js 文件中的单个字符串变量传递给 chrome 扩展 API 上的单独 javascript 文件?
- python - 如何使用python从文本中删除html标签?
- actions-on-google - 谷歌操作 - 调用相同的意图两次冻结
- c++ - LibCurl 和 Sql 命令
- java - Java - 如何使用 Executor Service 同时运行两个静态和 void 函数