首页 > 解决方案 > 如何从使用 nodejs 的直接链接下载文件

问题描述

嘿,我是 nodejs 的新手,我正在用它做一个应用程序管理器。

在应用程序管理器中,我想从直接链接下载文件,例如:

https://download.oracle.com/otn-pub/java/jdk/15.0.1%2B9/51f4f36ad4ef43e39d0dfdbaf6549e32/jdk-15.0.1_windows-x64_bin.zip

如果我单击该链接,我将下载文件,但我想在 NodeJS 中执行此操作。

谁能告诉我我该怎么做?

谢谢你帮助我。

标签: javascriptnode.js

解决方案


const http = require('http');
const fs = require('fs');

const url = “...” // whatever url you want
const filePath = “...” // where ever you want save the file along with the file name you want and extension example (here/file.zip)

const file = fs.createWriteStream(filepath);
const request = http.get(url, (response) => {
  response.pipe(file);
});

试试这个代码


推荐阅读