在node环境使用axios发送文件

时间:2022-10-19 16:58:01

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
})();
(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);
})();