0

I tried to send data with two inputs to a php code, one of them is just text and the other is type file. The second field also is for multiple selection of files. Well, here is the code of the html section:

        <input type="text" id="padron-documento" class="form-control input-lg" name="padron-documento" placeholder="Padrón" /><br />
        <div class="alert alert-info">
        <input type="file" id="archivos" name="archivos[]" multiple onChange="seleccionado()" />
        </div>
        <div id="cargados">
        <!-- Here we display the loaded files. -->
        </div>

Ok, this is the code of the javascript section:

function seleccionado(){
var archivos = document.getElementById("archivos");//We get the value of the input with id archivos
var archivo = archivos.files; //Here we get the value of input (archivos) into an array

//Teh FormData object allow us to create a form passing to it a key/value to send it, this kind of object already have the multipart/form-data to upload files
var datos = new FormData();

padron = $("#padron-documento").val();
//We don't know how much files the user will upload, we iterate the variable
//and we pass key/value with the method append to the object FormData, we use the index "i"    // to avoid repeated values, if we not use it, it will only have the value of the last iteration
for(i=0; i<archivo.length; i++){
datos.append('archivo'+i,archivo[i]);
}

$.ajax({
url:'subir.php', //Url
type:'POST', //Method
contentType:false, //It must be false
data: {padron,datos}, //We pass the object that we already create with archivos and also the text with id padron
processData:false, //It must be false to avoid jquery processing
cache:false //Avoid to save cache
}).done(function(msg)
{$("#cargados").append(msg); //It will show the files loaded into div "cargados"});}
/*****************************/

I have also a php file, who process the uploaded file/s, but it's works fine.Ok, my trouble it's going around the sending "data" attribute from the $.ajax object. How can I send a text chain such as ID number and also a file, both of them trough the data attribute of the $.ajax object, for example, I know that: data: id:idnumber,file:filearray, but I cannot make it work this way. The file is an array of files and the input text field is just that, text. Any help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

0 Answers0