简体   繁体   中英

How to automatically upload image from directory to axios in node.js

I am building a scenario where my web came takes photos continuously saves them in the folder and after i need to upload those files into the server via Axios.

const axios = require("axios");
const fs = require("fs");
const FormData = require("form-data");

function uploadImage(id) {
  console.log(id);
  let formdata = new FormData();
  formdata.append("image", fs.createReadStream("./Apartment.png"));

  console.log(formdata);
  axios({
    url:
      "https://api.fhg.ai/predict/image?{-apikey}",
    data: fs.createReadStream("./middleware/Apartment.png"),
    method: "POST",
  })
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }));
}

module.exports = uploadImage;

My api response send me error, and i in console log i do not see that i am uploading any image files.

let image = fs.createReadStream("./Apartment.png");

image.on('end', function() {
  const formData = new FormData();
  formData.append("file", image);

  const request = {
    method: "post",
    url: "https://api.fhg.ai/predict/image?{-apikey}",
    headers: {
      "Content-Type": "multipart/form-data"
    },
    data: formData
  };
  axios(request)
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }));;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM