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

javascript - Convert Buffer (image) to file - Stack Overflow

programmeradmin3浏览0评论

I'm looking for the best way to send image files to my server using Apollo Express, and Node. Getting the information there doesn't seem to be an issue, I convert the object into a string but can't find out how to convert it back to a regular file object to store away.

What I have so far;

JS - let buffer = await toBase64(file);

Through Apollo server..

Node - let buffer = Buffer.from(args.image, 'base64');

This gives me a Buffer. I'm unsure how to proceed with NodeJS to convert this back to a file object.

Thanks

I'm looking for the best way to send image files to my server using Apollo Express, and Node. Getting the information there doesn't seem to be an issue, I convert the object into a string but can't find out how to convert it back to a regular file object to store away.

What I have so far;

JS - let buffer = await toBase64(file);

Through Apollo server..

Node - let buffer = Buffer.from(args.image, 'base64');

This gives me a Buffer. I'm unsure how to proceed with NodeJS to convert this back to a file object.

Thanks

Share Improve this question asked Aug 16, 2022 at 12:01 dplumbonlinedplumbonline 511 silver badge8 bronze badges 1
  • to store, one would use fs, like nodejs/api/fs.html#filehandlewritefiledata-options – Lawrence Cherone Commented Aug 16, 2022 at 12:23
Add a ment  | 

2 Answers 2

Reset to default 3

I hope this will be helpfull for you

const file = new File([
      new Blob(["decoded_base64_String"])
    ], "output_file_name");

You can use one of the various write or writeFile methods which accept a Buffer.

const fs = require("fs");

let buffer = Buffer.from(
  "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAGCAIAAABxZ0isAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAQSURBVBhXY/iPAwygxP//AAjcj3EdtT3BAAAAAElFTkSuQmCC",
  "base64"
);

fs.writeFile("pic.png", buffer, (err) => {
  if (err) throw err;
  console.log("The file has been saved!");
});
发布评论

评论列表(0)

  1. 暂无评论