简体   繁体   English

在使用 axios 的 reactjs 中的 formDate 上使用 append 方法后,formData 正在设置从状态设置的空文件变量

[英]formData is setting empty file variable set from state after using append method on formDate in reactjs using axios

I am using formData with axios in my react frontend app in order to set the file using post method.我在我的反应前端应用程序中使用 formData 和 axios,以便使用 post 方法设置文件。 But, the data that I sent to the backend rest app is getting empty.但是,我发送到后端 rest 应用程序的数据变空了。


const formData = new FormData();
//setting the file upload value from state variable
formData.append("file", file);

Axios.post(url, formData, { headers: { 'Content-Type': 'multipart/form-data'}}).then(res => console.log(res))

It's hard to tell without seeing the backend code but I had this problem once, and the solution for me was that the key that you append the data to needs to be called the exact same thing in front- and backend, and if I'm not mistaken it's case sensitive aswell, which is where I believe my problem came from.如果没有看到后端代码就很难分辨,但我曾经遇到过这个问题,对我来说,解决方案是你附加数据的密钥需要在前端和后端被称为完全相同的东西,如果我是没记错,它也区分大小写,这就是我认为我的问题的来源。 So in your case the key is "file", and it needs to be called exactly "file" when you try to access it in your backend aswell.因此,在您的情况下,密钥是“文件”,当您尝试在后端访问它时,也需要将其准确地称为“文件”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM