Skip to content

Instantly share code, notes, and snippets.

@Kolesias123
Created June 25, 2013 21:22
Show Gist options
  • Save Kolesias123/5862543 to your computer and use it in GitHub Desktop.
Save Kolesias123/5862543 to your computer and use it in GitHub Desktop.
UploadPhoto: function(photo)
{
if ( API.Panel == null )
return;
// Obtenemos información de la foto.
photo = Photos.Data(photo);
// Al parecer esta foto era invalida.
if ( photo == undefined )
return;
// Mostrar la bara de progreso.
$('#me_photo').addClass('upload');
Info.ShowProgresBar('¡Estamos subiendo tu foto! <span class="progress">0</span>%', '.progress');
// Creamos un nuevo objeto de forumario.
var FD = new FormData();
FD.append('photo', photo);
FD.append('cookies', document.cookie);
// Realizamos la petición AJAX.
$.ajax({
url: Server['accounts'] + '/actions/ajax/upload.photo.php',
type: 'POST',
data: FD,
cache: false,
processData: false,
contentType: false,
xhr: function()
{
var XHR = jQuery.ajaxSettings.xhr();
if ( XHR.upload )
{
// Evento: Progreso de subida.
XHR.upload.addEventListener('progress', function(e)
{
// LOG!
console.log(e);
// No es posible calcular el tamaño, ocultar la barra de progreso y mostrar un mensaje.
if ( !e.lengthComputable )
{
Info.CloseProgressBar();
Info.ShowMsg('Subiendo archivo...', 0, true);
XHR.upload.onprogress = null;
}
var Loaded = e.position || e.loaded; // Lo que se ha cargado.
var Total = e.totalSize || e.total; // El total.
// Este navegador o el hosting no son compatibles...
if ( Total == 0 || Total == undefined )
return console.error('[UPLOAD] Ha ocurrido un error con el progreso.');
// Calculamos el porcentaje de subida.
var Progress = 100 / intval(Total) * intval(Loaded);
Progress = round(Progress);
// Lo actualizamos.
$('.progress').html(Progress);
if ( Info._Bar_Active == true )
{
$('.ProgressBar').val(Progress);
$('body, html').attr('style', ' ');
}
}, false);
}
return XHR;
},
success: function(e)
{
// Convertimos el resultado JSON a Array
result = Json.Parse(e);
// Un error...
if ( result.status == 'ERROR' )
{
$('#me_photo').removeClass('upload');
return Info.ShowMsg(result.message, 6000);
}
// Todo bien
else
{
// Cerramos la barra de progreso y actualizamos la foto.
Info.CloseProgressBar();
Info.CloseMsg();
API.UpdatePhoto();
}
},
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment