作为使用React,Express和Strapi的Web开发的新学习者。我想从我的React前端上传一个文件到Strapi,但是我的前端正在与Express Server通信,而Express与Strapi Server通信。
我确实设法将文件直接从react上载到Strapi,但是当我尝试使用中间的服务器时,总是会从Strapi收到400错误代码,对我而言,很难弄清楚如何处理{{1 }}。
快递:
fromdata
反应:
/* File upload */
app.post('/upload', async (req, res) => {
const data = req.files.files
console.log(data)
try {
await axios({
method: 'POST',
url: `${API_URL}/upload`,
data,
})
res.send()
} catch (error) {
res.send(error.response.status)
}
})
编辑
我为我的案例找到了一个简单的解决方案:Usig管道:
handleSubmitFile = async (e) => {
e.preventDefault();
const data = new FormData()
data.append('files', this.state.image)
console.log(data)
try {
const addArticleRes = await axios({
method: 'POST',
url: '/upload',
data,
})
//SUCCESS
if(addArticleRes.status === 200){ alert('Article created') }
} catch(error){
if(error.response.status === 404 || 400 ){
alert('oops something went wrong')
}
}
}