简体   繁体   English

Node.js Express将SVG文件流保存到文件

[英]Node.js Express save SVG file stream to file

I am new to Express and I need your help. 我是Express新手,需要您的帮助。

How to save SVG on server using Express? 如何使用Express在服务器上保存SVG?

const qr = require('qr-image');
const fs = require('fs');
exports.qr = function (req, res) {
   var code = qr.image(new Date().toString(), { type: 'svg' });

   // I would like to do something like this:
   fs.writeFile('qr.svg', code, (er)=> console.log(er)); 
   // and download using express.static


   res.type('svg');
   code.pipe(res);
};

Currently I am returning image as a stream and it works fine. 目前,我将图像作为流返回,并且工作正常。

I have an api with mongodb built on Express and I would like to store QR Codes on the server side. 我有一个基于Express构建的mongodb的api,我想在服务器端存储QR码。 Api is for the application built with Xamarin managing event tickets. Api适用于使用Xamarin管理事件票证的应用程序。

QR images are going to be downloaded more than once, that's why I would like to put them on server. QR图像将不止一次下载,这就是为什么我想将其放置在服务器上的原因。

Maybe better way would be to store them with SQLite locally on the client device? 也许更好的方法是将它们与SQLite本地存储在客户端设备上? Or maybe I should just send json data to be parsed into SVG? 或者,也许我应该只发送要解析为SVG的json数据?

How do you think? 你怎么想?

At the moment I am not implementing db locally. 目前,我还没有在本地实现db。

After some time when I went back to the topic I found the solution. 一段时间后,当我回到主题时,我找到了解决方案。 You need to add new action to your pipe. 您需要向管道添加新操作。 Simple as that: 就那么简单:

var code = qr.image(ticket.code, { type: 'svg' });
code.pipe(fs.createWriteStream('i_love_qr.svg'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM