将image/jpeg、image/png等文件转成multipart/form-data格式的node js

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

我有一个获取随机图像并将其发布到博客站点的应用程序。但是要为博客站点提供图像,它必须具有 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

您应该传递正确的header信息如下

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