I send the data by ajax and valid with jquery, then I receive the data in the php, to upload the archivo uso copy()
or move_uploaded_file()
and when the file is uploaded the browser is updated and what I need is for it to remain a response when uploading a file and not update, but when uploading the file is updated the browser can help me.
ajax and jquery code:
$("#Subir").on('click', function (e) {
e.preventDefault();
var file = $(".archi1")[0].files[0];
//obtenemos el nombre del archivo
var fileName = file.name;
//obtenemos la extensión del archivo
fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
if (fileExtension == "txt" || fileExtension == "csv") {
// if (fileName == 'MOVWEBDEF.txt' || fileName == 'MOVWEBDEF.csv') {
//aqui se muestra una imagen y se manda al url que se introduc el archivo .txt o .csv
$('#Cargando').css("display", "block");
formData = new FormData(document.getElementById("formulario"));
$.ajax({
url: "../../ActualizarRegistroCivil/ActualizarDef.php",
type: "POST",
dataType: "html",
data: formData,
cache: false,
contentType: false,
processData: false
})
.done(function (data) {
$("#Respuesta_Tabla").html(data);
if (data == 'Vacio') {
document.getElementById("Vacio").hidden = false;
document.getElementById("Vacio").style.color = "red";
$('#Cargando').css("display", "none");
} else if (data == 'FormatoIncorrecto') {
document.getElementById("FormatoInvalido").hidden = false;
document.getElementById("FormatoInvalido").style.color = "red";
$('#Cargando').css("display", "none");
} else if (data == 'Error') {
document.getElementById("Error").hidden = false;
document.getElementById("Error").style.color = "red";
$('#Cargando').css("display", "none");
} else {
$('#Cargando').css("display", "none");
document.getElementById("Respuesta_Tabla").hidden = false;
}
});
/* } else {
document.getElementById("ArchivoErroneo").hidden = false;
document.getElementById("FormatoInvalido").hidden = true;
document.getElementById("ArchivoErroneo").style.color = "red";
document.getElementById("Vacio").hidden = true;
}*/
} else {
$("#input01").each(function () {
this.value = "";
});
$(".form-control").each(function () {
this.value = "";
});
//mostramos el mjs de error
document.getElementById("FormatoInvalido").hidden = false;
document.getElementById("ArchivoErroneo").hidden = true;
document.getElementById("FormatoInvalido").style.color = "red";
document.getElementById("Vacio").hidden = true;
}
}
});
php:
$Archivo = $_FILES['archivo1']['name'];
$NombreArchivoText = $_FILES['archivo1']['tmp_name'];
if (empty($NombreArchivoText)) {
echo "Vacio";
} else {
if (is_uploaded_file($NombreArchivoText)) {
$formato = array('.txt', '.csv');
$ext = substr($Archivo, strrpos($Archivo, '.'));
if (in_array($ext, $formato)) {
if ($ext == '.txt') {
$formatos = '.txt';
}if ($ext == '.csv') {
$formatos = '.csv';
}
date_default_timezone_set("America/Costa_Rica");
$fecha = date_default_timezone_get();
$fecha = date("Y-m-d-H-i-s");
$NombreArchivo = "MOVWEBDEF" . '_' . $fecha . $formatos;
$ruta = "../Archivos/Defunciones/" . $Archivo;
move_uploaded_file($NombreArchivoText, $ruta); // al ejecutar esta linea sucede que el navegador se actualice
} else {
echo 'FormatoIncorrecto';
}
echo "guardo Archivo";
} else {
echo "Error";
}
}
html
<form enctype="multipart/form-data" id="formulario" method="POST">
<label>Subir Archivo extensiones: .txt ó .csv</label>
<div id="input1" hidden>
<input type="file" id="input01" class="archi1" name="archivo1" >
</div>
<div id="input2" hidden>
<input type="file" id="input02" class="archi2" name="archivo2" >
</div>
<div id="input3" hidden>
<input type="file" id="input03" class="archi3" name="archivo3">
</div>
<div id="input4" hidden>
<input type="file" id="input04" class="archi4" name="archivo4">
</div>
<div id="input5" hidden>
<input type="file" id="input05" class="archi5" name="archivo5">
</div>
<div id="FormatoInvalido" hidden="">
<br>
Extensión del archivo incorrecto
</div>
<div id="Vacio" hidden="">
<br>
Ingrese un archivo .txt ó .csv
</div>
<div id="Error" hidden="">
<br>
El archivo no fue subido por http
</div>
<div id="ArchivoErroneo" hidden="">
<br>
El archivo no pertenece a la categoría selecionada
</div>
<div class="Boton" >
<input name="Subir" id="Subir" class="BotonContinuar" type="button" value="Subir">
</div>
</form>