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

I'm looking for the best way to send image files to my server using Apollo Express, and Node.Gett

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!");
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744838903a4596433.html

相关推荐

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

    I'm looking for the best way to send image files to my server using Apollo Express, and Node.Gett

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信