Dear community, I told you that I made an sript to upload files to the server. Use html5, jquery / ajax and php. Up there all right, the issue is that now I want to get an alert type confirm in the case that the name of the file already exists on the server. The problem I have with the implementation of Ajax.
Here I leave my code, I think I have a logic error, since I am a lammer haha. Then I leave the ajax script:
$(function(){
$("#formuploadajax").on("submit", function(e){
e.preventDefault();
var f = $(this);
var formData = new FormData(document.getElementById("formuploadajax"));
formData.append("dato", "valor");
$('#target').hide();
$('#mensaje').html('<center><strong>¡Atención!</strong> El archivo se está subiendo. Esto puede demorar unos segundos...</center>');
//aca verifico si existe (en teoria :-p)
$.ajax({
url: "php/verificarArchivo.php",
type: "post",
dataType: "html",
data: formData,
cache: false,
contentType: false,
processData: false
success:function(r){
if(r==1){
//si existe pregunta si desea reemplazar
alertify.confirm('El archivo existe, ¿Desea reemplazar?',
function(){
$.ajax({
url: "php/uploadimage.php", //php para subir archivo
type: "post",
dataType: "html",
data: formData,
cache: false,
contentType: false,
processData: false
success:function(r){
if(r==1){
alertify.success("Actualizado con exito :)");
}else{
alertify.error("Fallo el servidor :(");
}
}
});
}, function(){ alertify.error('Se cancelo')};
//si no existe lo subo con uploadimage.php
} else {
$.ajax({
url: "php/uploadimage.php",
type: "post",
dataType: "html",
data: formData,
cache: false,
contentType: false,
processData: false
success:function(r){
if(r==1){
alertify.success("Actualizado con exito :)");
}else{
alertify.error("Fallo el servidor :(");
}
}
});
}
});
});
});
Sorry if your eyes hurt. The console does not throw me any errors but does not do any of the functions.
I also leave the script that works, but this one just uploads the file:
$(function(){
$("#formuploadajax").on("submit", function(e){
e.preventDefault();
var f = $(this);
var formData = new
FormData(document.getElementById("formuploadajax"));
formData.append("dato", "valor");
$('#target').hide();
$('#mensaje').html('<center><strong>¡Atención!</strong> El archivo
se está subiendo. Esto puede demorar unos segundos...</center>');
//formData.append(f.attr("name"), $(this)[0].files[0]);
$.ajax({
url: "php/uploadimage.php",
type: "post",
dataType: "html",
data: formData,
cache: false,
contentType: false,
processData: false
})
.done(function(res){
$("#mensaje").html("Listo!");
window.location.reload();
});
});
});
The idea would be that based on this code expand to check if there is previously the name in the bd. If so, ask if you want to replace. And in the case that it does not exist, upload it without consulting.