javascript 从 blob 在 nodeJs 中创建图像文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51709358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:39:21  来源:igfitidea点击:

Create image file in nodeJs from blob

javascriptnode.jspngblob

提问by Stevan Tosic

I am receiving BLOBdata on nodeJs server which is converted from PNG image.

我正在接收BLOB从 PNG 图像转换而来的 nodeJs 服务器上的数据。

I need to create again png image on nodeJs server to be able to show it on pdf document.

我需要在 nodeJs 服务器上再次创建 png 图像,以便能够在 pdf 文档上显示它。

I had tried to use FileSaver on nodeJs but it is not working. FileSaver works well on reactJs app.

我曾尝试在 nodeJs 上使用 FileSaver,但它不起作用。FileSaver 在 reactJs 应用程序上运行良好。

How can I save a new file to the local directory on the server?

如何将新文件保存到服务器上的本地目录?

There is a lot question pointing on problems with creating an image file form blob but I was unable to use base64encode, so other questions were not helpful.

有很多问题指向创建图像文件表单 blob 的问题,但我无法使用 base64encode,因此其他问题没有帮助。

采纳答案by Stevan Tosic

In BLOB data of png image file there is bufferproperty.

在 png 图像文件的 BLOB 数据中有buffer属性。

So I used this solution to create image.

所以我使用这个解决方案来创建图像。

var imageBuffer = request.file.buffer;
var imageName = 'public/images/map.png';

fs.createWriteStream(imageName).write(imageBuffer);

This has solved my problem.

这已经解决了我的问题。

回答by Jit Dhar

var base64Data = req.body.image.replace(/^data:image\/png;base64,/, "");

require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
  console.log(err);
});

Try this one here image is the name on which data is coming.

在这里试试这个图像是数据的名称。