$(function(){
var ul = $('#upload ul');
var intUploads = 0;
var intUploadsCompleted = 0;
var intUploadsError = 0;
$('#drop a').click(function(){
// Simulate a click on the file input button
// to show the file browser dialog
$('#fileinput').click();
//$(this).parent().find('input').click();
});
// Initialize the jQuery File Upload plugin
$('#upload').fileupload({
// This element will accept file drag/drop uploading
dropZone: $('#drop'),
maxNumberOfFiles: 1,
sequentialUploads: true,
limitConcurrentUploads: 1,
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function (e, data) {
intUploads++; //console.log('intUploads: ' + intUploads);
if ( intUploads < 2 )
{
var tpl = $('
');
// Append the file name and file size
tpl.find('p').text(data.files[0].name).append('' + formatFileSize(data.files[0].size) + '');
// Add the HTML to the UL element
data.context = tpl.appendTo(ul);
// Initialize the knob plugin
tpl.find('input').knob();
// Listen for clicks on the cancel icon
tpl.find('span').click(function(){
if(tpl.hasClass('working')){
jqXHR.abort();
}
tpl.fadeOut(function(){
tpl.remove();
$('#drop, .closeBtn').show();
});
if ( intUploads > 0 ) intUploads--;
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
}
},
progress: function(e, data){
// Calculate the completion percentage of the upload
var progress = parseInt(data.loaded / data.total * 100, 10);
// Update the hidden input field and trigger a change
// so that the jQuery knob plugin knows to update the dial
data.context.find('input').val(progress).change();
if(progress == 100){
data.context.removeClass('working');
}
},
send: function(e, data){
// Appena carica
$('#drop, .closeBtn').hide();
},
done: function(e, data){
// Ricarica pagina
var data_result = data.result;
var obj = jQuery.parseJSON( data_result );
//var obj = jQuery.parseJSON( data.result );
//alert( obj.id_immagine );
$('#id_immagine_crea').val( obj.id_immagine );
$('#crea-immagine').submit();
},
fail:function(e, data){
// Something has gone wrong!
data.context.addClass('error');
//$('#drop').show();
alert('Qualcosa รจ andato storto!');
if ( intUploads > 0 ) intUploads--;
},
submit:function(e, data){
$('#message-errore').html('').hide();
var blnReturn = true;
var blnTipo = true;
var strHTMLErrore = '';
var strFileName = data.files[0].name;
var extension = ( strFileName.substr( (strFileName.lastIndexOf('.') +1) ) ).toLowerCase();
if ( extension != 'jpg' && extension != 'jpeg' && extension != 'png' && extension != 'gif' )
{
blnReturn = false;
strHTMLErrore = 'Tipo di file non consentito.';
}
else
{
var intMaxSize = parseInt( $('#max_file_size').val() );
var intFileSize = data.files[0].size;
if ( intFileSize > intMaxSize )
{
blnReturn = false;
strHTMLErrore = 'Dimensioni del file eccessive.';
}
}
if ( ! blnReturn )
{
$('#upload li').remove();
$('#message-errore').html( strHTMLErrore ).show();
$('#drop').show();
return false;
}
}
});
// Prevent the default action when a file is dropped on the window
$(document).on('drop dragover', function (e) {
e.preventDefault();
$('#drop').addClass('q_drag');
});
$(document).on('drop dragleave', function (e) {
$('#drop').removeClass('q_drag');
});
// Helper function that formats the file sizes
function formatFileSize(bytes) {
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes / 1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes / 1000000).toFixed(2) + ' MB';
}
return (bytes / 1000).toFixed(2) + ' KB';
}
});