首页 > 解决方案 > 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)

标签: node.jsfs

解决方案


试试这个代码,为此,你需要安装额外的包,比如 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()

推荐阅读