简体   繁体   English

使用fetch api发送带有图像的数据,但文件数组为空php

[英]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.

相关问题 如何使用 fetch API 同时将上传的文件(图像、pdf)和 JSON 数据传递到后端? - How to pass uploaded files(images, pdf) and JSON data to the backend at the same time using fetch API? 获取 Api - 如何将 PHP SESSION 数据发送到目标 Z2FEC392304A5C23AC138DA27 文件? - Fetch Api - how to send PHP SESSION data to the target PHP file? 如何在 Fetch API 上将带有输入文件的数组发送到 PHP - How to send a array with input file to PHP on Fetch API 数组 $data ['image'] 显示为空数组,无法将文件发送到 php - the array $data ['image'] is displayed as an empty array, it is not possible to send a file to php 在 React Native 中使用 Fetch API 将数据发送到服务器 - Send Data to a Server using Fetch API in React Native 如何使用FETCH API将数据发送到Spring Boot方法 - How to send data to spring boot method using FETCH API 使用 JavaScript Fetch 和 POST 发送表单数据,然后在 PHP 中处理它 - Send form data using JavaScript Fetch and POST and then process it in PHP 如何使用Javascript向PHP服务器发送空数组 - How to send an empty array to PHP server using Javascript 如何使用JSON将空数组从JS发送到PHP? - How to send an empty array from JS to PHP using JSON? 使用 Jquery 和 Fetch 将多个文件发送到 PHP - Send Multiple Files to PHP with Jquery and Fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM