简体   繁体   中英

Convert image/jpeg, image/png, etc. files to multipart/form-data format node js

I have an application that gets random image and posts it to blog site. But to give a blog site an image it has to have a type of multipart/form-data.

  const FormData = require('form-data')
  const form = new FormData()

  const { data } = await axios.get(
    'https://photo-works.net/images/europe-landscape-photo-edited.jpg'
  ) // image/jpeg

  form.append('my-photo', data)
  const photo = await axios.post(url, { photo: form })   // that should be multipart/form-data format

You should pass right header information as follows

var formData = new FormData();
formData.append("my-photo", data);
axios.post('upload_file', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})

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