fs.createwritestream 不是函数

fs.createwritestream is not a function

我正在尝试开发一个 React 应用程序来帮助我将文件下载到我的亚马逊云端。 为此,我使用了这个模块

download-file-with-progressbar

这是我的代码:

      var dl            = require('download-file-with-progressbar');
                //-----------Download File-------------------
                option = {
                    filename: 'fileName',
                    dir: os.tmpdir(),
                    onDone: (info)=>{
                    console.log('done', info);
                    },
                    onError: (err) => {
                    console.log('error', err);
                    },
                    onProgress: (curr, total) => {
                    console.log('progress', (curr / total * 100).toFixed(2) + '%');
                    },
                }

                dd = dl('https://nodejs.org/dist/v6.10.2/node-v6.10.2-linux-x64.tar.gz', this.option);
                //-------------------------------------------

它在 nodejs 项目上运行良好。但是当我在一个反应​​项目(使用 'create-react-app' 创建)中使用它时,它产生了以下错误。

TypeError: fs.createWriteStream is not a function

我对该项目的想法是创建一个 React 应用程序,帮助将文件下载到我的服务器或云 space。如何修复该错误。

createWriteStream 是 Nodejs API 的功能部分,这就是为什么当您 运行 它作为 nodejs 项目时它工作正常。

但是当你 运行 一个 React 应用程序时,执行你的代码的是你的浏览器,而不是 nodejs。浏览器不知道这个 API.

如果你想从 React 应用程序下载文件,你必须通过 HTTP 请求来完成。例如,您可以使用 fetch API