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

时间:2013-05-24 20:21:39

标签: php jquery ajax upload multipartform-data

我希望使用带有此代码的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;

此行包含表单中所有字段的数据:

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

但在PHP中,我无法访问该数据,我希望访问表格数据以便在表格中插入,你知道这是什么问题吗?

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>

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

enter image description here

1 个答案:

答案 0 :(得分:1)