javascript - 如何从使用 nodejs 的直接链接下载文件
问题描述
嘿,我是 nodejs 的新手,我正在用它做一个应用程序管理器。
在应用程序管理器中,我想从直接链接下载文件,例如:
https://download.oracle.com/otn-pub/java/jdk/15.0.1%2B9/51f4f36ad4ef43e39d0dfdbaf6549e32/jdk-15.0.1_windows-x64_bin.zip
如果我单击该链接,我将下载文件,但我想在 NodeJS 中执行此操作。
谁能告诉我我该怎么做?
谢谢你帮助我。
解决方案
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);
});
试试这个代码
推荐阅读
- python - 从文件而不是完整文件导入特定类是否重要?
- oracle-adf - 如何在按钮单击时从片段重定向到另一个 .jsf 页面
- jquery - jQuery 验证动态创建的 reCaptcha 表单
- python-3.x - 导入错误:python 中没有名为 pyimgur 的模块
- python - 如何从熊猫的给定行中删除仅包含零的列
- amazon-web-services - 数组中的突变响应我想在 AppSync 控制台的订阅中显示突变数组响应
- typescript - Typescript Angular中带有索引的最后状态数组
- angular - 垫表可扩展行 Angular 7
- python - 正则表达式 findall python
- java - 如何在 RecyclerView 中选择多个项目并将它们放入 List