未捕获的TypeError:fs.​​createWriteStream不是函数 [英] Uncaught TypeError: fs.createWriteStream is not a function

查看:712
本文介绍了未捕获的TypeError:fs.​​createWriteStream不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在客户端制作pdf文件,之后必须通过REST调用发送服务器端。

I want to make a pdf file on client-side and afterwards it has to be send server-side through a REST call.

生成文件, 我没有问题。我使用pdfMake生成文件,并且工作正常。

With the generation of the file, I have no problem. I use pdfMake to generate the file and that works fine.

因为我想将文件发送到我的后端,所以我必须将文件存储在文件系统中。为此,我使用node.js.下面的代码工作正常。只有当我想使用对象fs的函数时,才会出现错误'Uncaught TypeError:fs.​​createWriteStream不是函数'。我做错了什么?

Because I want to send the file to my backend, I have to store the file on the filesystem. To achieve this, I use node.js. The code below works fine. Only when I want to use a function of the object fs, I get the error 'Uncaught TypeError: fs.createWriteStream is not a function'. What am I doing wrong?

var docDefinition = localStorage.getItem('docDefinition');  
var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter();

var pdfDoc = printer.createPdfKitDocument(JSON.parse(docDefinition));

var fs = require('fs');
var stream = fs.createWriteStream('basics.pdf');
pdfDoc.pipe(stream);
pdfDoc.end();


推荐答案

您正在混淆服务器和客户端。浏览器没有编程访问文件系统。期。文件系统api仅通过用户交互(例如在网页上拖放文件)触发。您无法在客户端本地写入文件。

You are confusing the server and the client. The browser has no programmatic access to the filesystem. Period. The filesystem api is triggered only via user interaction (like dragging and dropping a file on the webpage). You cannot write a file locally on the client.

这篇关于未捕获的TypeError:fs.​​createWriteStream不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆