将base64图像转换为Node Js中的文件 [英] Convert base64 image to a file in Node Js

查看:155
本文介绍了将base64图像转换为Node Js中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node Js的新手。我需要为用户添加个人资料图片。我从IOS app获得了base64中的图像请求。我需要将它存储在images文件夹中并将图像路径保存在mongodb数据库中。

I am new to Node Js. I need to include a profile image for users. I get request of image in base64 from IOS app. I need to store it in images folder and save the image path in mongodb database.

我使用了以下代码,

var bitmap = new Buffer(req.body.profile_image, 'base64');
// write buffer to file
fs.writeFileSync("images/example.jpg", bitmap);

其中req.body.profile_image是base64图片。

where req.body.profile_image is a base64 image.

我收到以下错误,

TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.

req.body.profile_image值将是,

req.body.profile_image value will be,

+MZScHeJQ9Cz5dfxnEmIMHWMZyZYnYx8Rrhj0HbtfGUanv5w3UHUyADbiGsKJxeM1yV4uGwBS7cYlAec1w0AX6xg2A1O854UF8OS6PAP1MtzkeFnrNlD41U8XFeGrp1fn3jRMUs8sqS61umSS2rR2NDhppjZ4OvnOWBAq6X+sQNkhKkfZOdYsZOpz8fWIQb6wQ/GchVCgfZko4PMDg1DSumausG6o+2E6wKLLjKReUaHEQXKJV8h85XEKN4p/WEBvTHmmJ/IN178YJVgrGmfOScAuBPp+sggGA7/wC1kgbDiacbGABOcCLHVRpMuBQh5Xn4xqARF03pwkJT23LhxGLiSGp8mCVWDrzPf3iwp4C3nDSg2VUfNwgDvm6vrIiFJvp8ZHIdjoFx8BX0OH0+8TVii3GAKKc2kjz7dYqUCdsuMOm2hrr+h//Z

请帮忙。

推荐答案

编辑:这段代码对我有用。也许错误会在以后发生。

This code worked for me. Maybe the error happens later.

var fs = require("fs");
var image = "+MZScHeJQ9Cz5dfxnEmIMHWMZyZYnYx8Rrhj0HbtfGUanv5w3UHUyADbiGsKJxeM1yV4uGwBS7cYlAec1w0AX6xg2A1O854UF8OS6PAP1MtzkeFnrNlD41U8XFeGrp1fn3jRMUs8sqS61umSS2rR2NDhppjZ4OvnOWBAq6X+sQNkhKkfZOdYsZOpz8fWIQb6wQ/GchVCgfZko4PMDg1DSumausG6o+2E6wKLLjKReUaHEQXKJV8h85XEKN4p/WEBvTHmmJ/IN178YJVgrGmfOScAuBPp+sggGA7/wC1kgbDiacbGABOcCLHVRpMuBQh5Xn4xqARF03pwkJT23LhxGLiSGp8mCVWDrzPf3iwp4C3nDSg2VUfNwgDvm6vrIiFJvp8ZHIdjoFx8BX0OH0+8TVii3GAKKc2kjz7dYqUCdsuMOm2hrr+h//Z";
var bitmap = new Buffer(image, 'base64');
fs.writeFileSync("images/example.jpg", bitmap);

如果你说

console.log(req.body.profile_image) 

而不是

 console.log(typeof req.body.profile_image)

它会在打印前将req.body.profile_image中的数据转换为字符串。你可能只是在评论时忘记了'typeof',但是如果你没有添加'typeof',你就不能确定它包含一个字符串。缓冲区有多个构造函数,它可能使用了错误的构造函数。

It would cast what ever data is in req.body.profile_image to a string before printing. Its possible you just forgot the 'typeof' when commenting, but if you didn't add the 'typeof' you can't be certain that it contains a string. There's more than one constructor to Buffer and it may be using the wrong one.

对于lolz尝试:

Buffer.from(String.fromCharCode.apply(null, new Uint16Array(req.body.profile_image)), "base64")






什么被分配给req.body.profile_image?


What gets assigned to req.body.profile_image?

writeFileSync的第一个参数是一个字符串,因此它不会导致类型错误。你能更具体地了解req.body.profile_image中包含的内容吗?

The first argument to writeFileSync is a string, so it can't be causing the type error. Can you be more specific as to what is contained in req.body.profile_image?

在旁注中,虽然我认为这与您的问题无关,但使用'new'关键字实例化缓冲区是已弃用
也许使用 Buffer.from(...)会让你感动解决方案的方向。

On a side note, although I think this is unrelated to your problem, instantiating a Buffer with the 'new' keyword is deprecated. Maybe using Buffer.from(...) will move you in the direction of a solution.

这篇关于将base64图像转换为Node Js中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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