php 使用 jQuery ajax 通过 formData() 上传文件和表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16743045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:38:45  来源:igfitidea点击:

Using jQuery ajax to upload file and form data with formData()

phpjqueryajaxuploadmultipartform-data

提问by SoldierCorp

I want upload a file with jquery ajax and php using forData() with this code:

我想使用以下代码使用 forData() 上传带有 jquery ajax 和 php 的文件:

var data = new FormData();

data.append('image',document.getElementById('uFile').files[0]);
data.append('tag','saveDocument');
data.append('data',$('#saveDocument').serializeArray());

$.ajax({
    url: url,
    type: 'post',
    data: data,
    cache: false,
    contentType:false,
    dataType: 'json',
    processData: false,
    success: function (data) {
        setAlert("Documento guardado correctamente!",success);
    }, error: function() {
        setAlert("Ha ocurrido un error al guardar!",error);
    }
});
return false;

This line contains all data of fields in my form:

这一行包含我表单中字段的所有数据:

data.append('data',$('#saveDocument').serializeArray());

But in PHP I can't access to that data and I want access to data of form to insert on a table, do you know what's the problem?

但是在 PHP 中,我无法访问该数据,而我想访问要插入表中的表单数据,您知道问题出在哪里吗?

Html Form

html表单

<form id="saveDocument" enctype="multipart/form-data" method="post">
<p><i>Todos los campos son requeridos!</i></p>
<p> 
    <input id="uName" class="uName span5" name="uName" type="text" placeholder="Nombre completo" required/>
</p>
<p> 
    <input id="uEmail" class="uEmail span5" name="uEmail" type="email" placeholder="E-mail" required/>
</p>
<p> 
    <select id="uDept" class="uDept span5" name="uDept" type="text" required>
        <option value="0">Seleccione departamento</option>
        <option value="1">Dirección</option>
        <option value="2">Recursos Humanos</option>
        <option value="3">Oficina</option>
    </select>
</p>
<p> 
    <input id="uIssue" class="uIssue span5" name="uIssue" type="text" placeholder="Asunto" required/>
</p>
<p>
    <textarea id="uComment" class="uComment" name="uComment" placeholder="Comentario (Máximo 30 caracteres)" required></textarea>
</p>
<p>
    <select id="uUrgency" class="uUrgency span5" name="uUrgency" type="text" required>
        <option value="0">Seleccione urgencia</option>
        <option value="1">Normal</option>
        <option value="2">Alta</option>
        <option value="3">Urgente</option>
    </select>
</p>
<p>
    <input id="uFile" class="uFile span5" name="uFile" type="file" required/>
    <input id="nameFile" class="nameFile span5" name="nameFile" type="text" placeholder="Click para seleccionar el archivo" onClick="$('.uFile').click();"/>
</p>
<p>
    <input class="btn btn-danger" type="reset" value="Limpiar"/>
    <input id="sendFile" class="btn btn-primary" type="submit" value="Guardar"/>
</p>

The below image is taken from developers tool of chrome:

下图来自chrome的开发者工具:

enter image description here

在此处输入图片说明

回答by botenvouwer