Strapi 处理上传文件
Strapi 处理上传文件-axios
const axios = require(‘axios’);
const FormData = require(‘form-data’);
const fs = require(‘fs’);
let data = new FormData();
data.append(‘files.img’, fs.createReadStream(‘/Users/originbit/Desktop/截屏2024-07-12 11.43.50.png’));
data.append(‘files.img’, fs.createReadStream(‘/Users/originbit/Desktop/截屏2024-07-12 11.43.50 2.png’));
data.append(‘data’, ‘{“name”:“888”}’);
let config = {
method: ‘post’,
maxBodyLength: Infinity,
url: ‘127.0.0.1:1337/api/testimgs’,
headers: {
‘Authorization’: ‘••••••’,
…data.getHeaders()
},
data : data
};
axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
注意事项
以form-data上传,文件字段名字设置为“files”后缀,数据放置在data对象里面