[英]$_FILES is empty when sending a POST request using axios in ReactJs
I have an array of Files that i want to retrieve in php along with some other string params, When i send the file(s) from the FormData
in React they're being received as json in php:我有一个文件数组,我想在 php 以及其他一些字符串参数中检索这些文件,当我从 React 中的
FormData
发送文件时,它们在 ZE1BFD762321E409CEE4AC0B6E84 中被接收为 json:
"{"dataForm":{"Files":[{"path":"aust.jpeg"}]},"headers":{"content-type":"multipart/form-data"}}"
I want to receive them in $_FILES instead for obvious reasons, is it possible to do that and read the rest of the params as json in php?出于明显的原因,我想在 $_FILES 中接收它们,是否可以这样做并将参数的 rest 读取为 php 中的 json? Also my php.ini is fully configured to allow file uploads
我的 php.ini 也完全配置为允许文件上传
Here is the Reactjs code:这是 Reactjs 代码:
import axios from 'axios';
const sendMail = (data, uploadedFiles) => {
const dataForm = new FormData();
dataForm['Files'] = uploadedFiles; // Here uploadedFiles is an array of files
console.log(dataForm['Files'])
axios.post('http://localhost/bickdata/mail/SendMail.php', {
dataForm,
headers: {
'content-type': 'multipart/form-data'
}
}) .then(function (response) {
console.log(response);
});
}
Thanks in advance !提前致谢 !
Turns out axios.post()
sends all the data as JSON by default which is why the files are not being interpreted as File objects in php, I did some minor changes with the post request and i finally received my files, here's the updated code:结果是
axios.post()
默认情况下将所有数据作为 JSON 发送,这就是为什么文件没有被解释为 ZE1BFD762321E409CEE4AC0B6E409CEE4AC0B6E841963CZ 中的文件对象的原因,我终于收到了更新后的文件:
(i'm only sending one file from the array for now, but i'm pretty sure it's the same procedure for an array of files) (我现在只从数组中发送一个文件,但我很确定对于一组文件来说这是相同的过程)
dataForm.append(
"potato",
uploadedFiles[0],
uploadedFiles[0].name
);
axios({
method: 'post',
url: 'http://localhost/bickdata/mail/SendMail.php',
data: dataForm,
headers: {'Content-Type': 'multipart/form-data' }
})
.then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
This is how you upload multiple files in React这就是你在 React 中上传多个文件的方式
const dataForm = new FormData();
uploadedFiles.map(file =>
dataForm.append("Files[]", file);
}
In my case, it was 2MB upload_max_filesize in php.ini.就我而言,php.ini 中的 upload_max_filesize 为 2MB。 Please see PHP - empty $_POST and $_FILES - when uploading larger files I pulled out my hair for 2 hours.
请参阅PHP - 为空 $_POST 和 $_FILES - 上传较大的文件时,我拔了 2 个小时的头发。 Nothing wrong with Axios.
Axios 没有问题。 I didn't need to specify headers.
我不需要指定标题。
Ref MDN Web Docs , the FormData's encoding type were set to multipart/form-data
.参考MDN Web Docs ,FormData 的编码类型设置为
multipart/form-data
。 Taking the sample code cue, I managed to upload a file with this:以示例代码为线索,我设法上传了一个文件:
const api = axios.create({ baseURL: '<?php $this->getUrl() ?>' })
editPost = function(e) {
// target is the input element
const target = typeof e === 'string' ? document.querySelector(e) : e.target;
let form = new FormData();
form.append(target.name, target.type == 'file' ? target.files[0] : target.value);
form.append('sid', '<?php echo $this->getSessionId() ?>');
api.post('/editPost', form)
.then((response) => {
//...
})
.catch(() => {
//...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.