最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: fs.createWriteStream is not a function - Stack Overflow

programmeradmin1浏览0评论

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

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

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();

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

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

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();
Share Improve this question asked Jun 1, 2016 at 13:09 user3193324user3193324 1031 gold badge1 silver badge4 bronze badges 3
  • 1 Are you using fs.createWriteStream in the browser? – robertklep Commented Jun 1, 2016 at 13:16
  • 1 @robertklep I assume from the mix of fs and localStorage that such is indeed the case – Jared Smith Commented Jun 1, 2016 at 13:21
  • I'm using it in the browser. The front-end is made with angularjs and jquery. It retrieves the data from a Spring Rest webservice. – user3193324 Commented Jun 1, 2016 at 13:38
Add a comment  | 

1 Answer 1

Reset to default 16

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.

发布评论

评论列表(0)

  1. 暂无评论