在node环境使用axios发送文件

yarn add form-data

(async () => {
  const l = console.log;
  const axios = require("axios");
  const fs = require("fs-extra");
  var FormData = require("form-data");
  var form = new FormData();
  form.append("file", fs.createReadStream("C:\\Users\\Administrator\\Pictures\\a.jpg"));
  form.append("type", "avatar");

  let r = await axios({
    method: "post",
    url: "http://localhost:5000/upload",
    data: form,
    headers: form.getHeaders()
  }).then(v => v.data);

  l(r); // ok
})();
posted @ 2018-10-20 20:38  Ajanuw  阅读(1450)  评论(0编辑  收藏  举报