[英]Send data with images using fetch api but array of files is empty php
my intention is to try pass a json to php using fetch api, all the data from the json pases except the data from the files and i used var dump to check the data that i was reciveing, why cant i not get the files data?我的意图是尝试使用 fetch api 将 json 传递给 php,来自 json 的所有数据都传递给文件,除了文件中的数据,我使用 var dump 来检查我正在接收的数据,为什么我不能获取文件数据? post data: i did not copy all the inpust of the form
发布数据:我没有复制表格的所有输入
<form method="post" id='almacenInfo' enctype="multipart/form-data" name='almacenInfo'>
<label for="NombreInput">Nombre de producto</label>
<input type="text" class="form-control" id="NombreInput" placeholder="Nombre">
<div id='content_files'>
<button type="button" class="subir_archivos fa fa-upload">
<input id="upload_input" type="file" name="archivos" filename="name" accepted=".png,.jpg,.jpeg,.svg">
</button>
</div>
<div class="modal-footer">
<button type='submit' class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</form>
var img = []
form.addEventListener('submit', function(e){
e.preventDefault();
let data = {
opcion: form.getAttribute('modalTipo'),
idProducto: form.getAttribute('idProducto'),
nombre: document.getElementById('NombreInput').value,
precio: document.getElementById('PrecioInput').value,
cantidad: document.getElementById('CantidadInput').value,
categoria: document.getElementById('EtiquetasInput').value,
descripcion: document.getElementById('descripcionInput').value,
img: img
};
console.log(data);
fetchData(data);
});
document.getElementById('upload_input').addEventListener('change', function(e){
if (this.files.length > 0) {
let fileName = this.files[0].name;
let exists = false;
img.forEach(i => {
if (i.name == fileName) {
exists = true;
}
});
console.log(exists);
if (exists) {
console.log('ya existe esta imagen');
} else{
document.getElementById('content_files').append(fileName) ;
img.push(this.files[0]);
}
}
})
<?php
include_once('./conexion.php');
include_once('./apis/apiAlmacen.php');
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
var_dump($obj);
?>
json in front end when i do a console log of data :当我做数据的控制台日志时,前端的 json :
var dump of $obj in backend:后端 $obj 的 var 转储:
I think you get null because the value img is a binary file when you read it with php read ,我认为你得到 null 因为当你用 php read读取它时,值 img 是一个二进制文件,
So try to use FormData() to create the correct Json full example所以尝试使用 FormData() 创建正确的 Json完整示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.