xmlHttp_uploadFileInsert = ajaxObj(); xmlHttp_uploadFileUpdate = ajaxObj(); xhr = ajaxObj(); uploadFile = function(t,id,type) { document.getElementById('camera'+id+'_img').src = 'images/ajax-loader-transparent.gif'; document.getElementById('button_'+id).className = 'pressed'; var canceled = 0; var nId = ''; xmlHttp_uploadFileUpdate.abort(); xmlHttp_uploadFileUpdate = ajaxObj(); if (new XMLHttpRequest().upload) { var files = t.files; var numFiles = files.length; var file; for (var j = 0; j < numFiles; j++) { file = files[j]; } var fileTypes = {"application/pdf":"pdf","image/png":"image","image/x-png":"image","image/gif":"image","image/jpeg":"image","image/pjpeg":"image","image/tif":"image","image/tiff":"image"}; if (fileTypes[file.type]) { // xhr = new XMLHttpRequest(); xhr.abort(); xhr = ajaxObj(); var fd = new FormData(); xhr.upload.addEventListener("progress", function(e) { if (e.lengthComputable) { if (canceled != 1) { var percentage = Math.round((e.loaded * 100) / e.total); xmlHttp_uploadFileUpdate.abort(); xmlHttp_uploadFileUpdate = ajaxObj(); xmlHttp_uploadFileUpdate.onreadystatechange=function() { if(xmlHttp_uploadFileUpdate.readyState==4 && xmlHttp_uploadFileUpdate.status==200) { } } if (nId != '') { xmlHttp_uploadFileUpdate.open("GET","modules/updateFileInDb.php?id="+nId+"&p="+percentage,true); xmlHttp_uploadFileUpdate.send(null); } document.getElementById('percentage_'+id).style.width = percentage+'%'; } } }, false); xhr.open("POST", "modules/uploadFile.php", true); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { document.getElementById('camera'+id+'_img').src = 'images/camera-white-20.png'; document.getElementById('button_'+id).className = 'normal'; document.getElementById('button_'+id).style.backgroundColor = '#434343'; document.getElementById('percentage_'+id).style.width = '0%'; document.getElementById('camera'+id).value = ''; } if (xhr.readyState == 4 && xhr.status == 200) { // alert(xhr.responseText); } else if (xhr.readyState == 4 && xhr.status != 200) { xhr.abort(); xmlHttp_uploadFileInsert.abort(); xmlHttp_uploadFileUpdate.abort(); alert('Skipanin fær ikki goymt fíluna.'); canceled = 1; // location.reload(); } }; /* Leggur fílunavnið í dátugrunnin */ xmlHttp_uploadFileInsert.abort(); xmlHttp_uploadFileInsert = ajaxObj(); xmlHttp_uploadFileInsert.onreadystatechange=function() { if(xmlHttp_uploadFileInsert.readyState==4 && xmlHttp_uploadFileInsert.status==200) { if (xmlHttp_uploadFileInsert.responseText != '') { nId = xmlHttp_uploadFileInsert.responseText; fd.append('type', type); fd.append('id', nId); fd.append('uploadAttachments', file); // Initiate a multipart/form-data upload xhr.send(fd); } } } xmlHttp_uploadFileInsert.open("GET","modules/addFileToDb.php?name="+file.name+"&size="+file.size+"&type="+type,true); xmlHttp_uploadFileInsert.send(null); } else { alert('Fílan skal vera av slagnum .pdf, .png, .gif, .jpg, .jpeg, .tif ella .tiff'); document.getElementById('uploadAttachmentButton').className = ''; document.getElementById('uploadAttachmentButton').style.cursor = 'pointer'; document.getElementById('uploadAttachmentButton').firstChild.nextSibling.style.cursor = 'pointer'; document.getElementById('uploadAttachmentButton').style.backgroundColor = '#434343'; t.reset(); } } }