-1

I got this third party lib which generates a screenshot.

enter image description here

I want to save this on my server. I'm using Axios.It's probably something with blobs, arraybuffers etc?

  1. How do I send it?

    Axios.post('/api/saveimage', { ??? })

  2. Using NodeJs express on backend. How do I save this to a physical image file?

Joe
  • 4,274
  • 32
  • 95
  • 175
  • 1
    Try popping `data.dataUrl` into a base64 to image convertor (a quick Google would turn this up: https://codebeautify.org/base64-to-image-converter), then once you're sure it's base64 data you're dealing with, a quick google will give you an npm package that specialises in turning base64 data into an image – MCMXCII Apr 13 '22 at 17:06

1 Answers1

0

Well at the frontend you need to send it like this:

let formData = new FormData()

formData.append("image", file)

axios.post("/api/saveimage",formData)

At the first step you create a FormData, then you append the file. In this case i named the file image. Now lets go to the next step. You will need multer on your nodejs side.

npm i multer

The first think you need to do, is to create an middleware:

const multer = require("multer");
const whitelist = ["image/png", "image/jpeg", "image/jpg", "image/webp"];
const storeImages = multer.diskStorage({
  destination: async function (req, file, cb) {
    if (!whitelist.some((type) => type === file.mimetype)) {
      return cb(new Error("File is not allowed"), "/");
    }

    cb(null, "your/destination/path");
  },
  filename(req, file, cb) {
    let [name] = file.originalname.split(".");

    cb(null, name + ".png");
  },
});

exports.uploadImageStorage = multer({
   storage: storeImages,
});

Here watch out: Your destination path should exist. Also dont forget an extension for your file in this case .png

Now you create your route:

const { uploadImageStorage } = require("../yourstorage")
app.post("/api/saveimage", uploadImageStorage.single("image"), (req, res) => {
   let file = req.file
   let path = file.path
})

Here you need to know at uploadImageStorage.single("image") i used image like i used it in formData.append("image", file) they need to be the same.

Now you can save the path of your file into a database. You can also transform your image with sharp if you want

From my experience if you have folder called static and you have a image inside of it like photo.png you usually get the photo with localhost:3000/photo.png and not with localhost:3000/static/photo.png

You will need to remove static from your path if you have this setup. Otherwise if you try to display the image on the frontend you wont see it.

bill.gates
  • 14,145
  • 3
  • 19
  • 47
  • Thanks for answer. In formData.append("image", file), what is file? Is that the content in the screenshot in my question? All of it or part of it? – Joe Apr 13 '22 at 18:27
  • @Joe its the file from your change event. You can get the file from the event with `let [file] = event.target.files` – bill.gates Apr 13 '22 at 18:30
  • As i said in my question, the screenshot is generated from a third party lib. I don't upload it myself. The content I receive, I posted in the question. – Joe Apr 13 '22 at 18:35
  • @Joe then convert your DataUrl to an file. If you google for "convert dataurl to file" you will get this at the top result https://stackoverflow.com/questions/6850276/how-to-convert-dataurl-to-file-object-in-javascript – bill.gates Apr 14 '22 at 12:19