//upload2_error uploaded2_started upload2_notstarted upload2_completed upload2_server upload2_default
var file_item_html = '
' +
'
previewcontent
' +
'
x
' +
'
' +
'previewcontent' +
'
file_size
' +
'
' +
'
' +
'
';
function file_item_click(id) {
if (id.indexOf("server_") === -1)
return;
jQuery("#" + id).parent().children(".selectedsinglefile").removeClass("selectedsinglefile");
jQuery("#" + id).addClass("selectedsinglefile");
}
var uploader2_files_queue = [];
var uploader2_xhr = null;
function get_file_item_html(file_item_id, filename, filesize, filetype, path) {
if (filesize > 1048576) {
filesize = Math.round(filesize / 1048576) + " MB";
} else if (filesize > 1024) {
filesize = Math.round(filesize / 1024) + " KB";
} else {
filesize = "" + filesize + " Byte";
}
if (path === null)
path = "";
var previewcontent;
if (path === null || path === '') {
previewcontent = "";
} else {
previewcontent = "";
}
var logo = find_logo(filetype, path, filename);
var tt = file_item_html.split('uploaderid_file_id').join(file_item_id);
tt = tt.split('file_name').join(filename);
tt = tt.split('file_size').join(filesize);
tt = tt.split('linkhref').join(path);
tt = tt.split('previewcontent').join(logo);
return tt;
}
function add_new_items_2_list(uploaderid, filetypes, tablename, tableid, maxsize, max_file_count) {
var file_list_id = uploaderid + "_filelist";
var file_list_selector = "#" + file_list_id;
var file_input_id = uploaderid + "_file";
var files = document.getElementById(file_input_id).files;
var currentindex = jQuery(file_list_selector).children().length;
if (files === null) {
return;
}
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (maxsize < file.size && maxsize !== -1) {
var filesize = maxsize;
if (filesize > 1048576) {
filesize = Math.round(filesize / 1048576) + "MB";
} else if (filesize > 1024) {
filesize = Math.round(filesize / 1024) + "KB";
} else {
filesize = "" + filesize + "Byte";
}
var message = "فایل " + file.name + " اضافه نشد" + "
" +
"حجم فایل انتخابی باید کمتر از " + filesize + " باشد";
alertbox(message);
continue;
}
if (currentindex >= max_file_count && max_file_count != -1) {
alertbox("حداکثر می توانید " + max_file_count + "فایل ارسال کنید" + "
" +
"برای افزودن فایل جدید یکی از فایلها را حذف کنید");
return;
}
var extension = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase();
if (filetypes.indexOf("*") < 0 && filetypes.indexOf(extension) < 0) {
alertbox("نوع فایل مجاز نیست");
console.log(extension);
console.log(filetypes);
continue;
}
var file_item_id = "client_" + uploaderid + "_" + currentindex;
var file_item_selector = "#" + file_item_id;
jQuery(file_list_selector).prepend(get_file_item_html(file_item_id, file.name, file.size));
jQuery(file_item_selector).addClass("upload2_notstarted");
uploader2_files_queue.push({file: file, uploaderid: uploaderid, tablename: tablename, tableid: tableid, file_item_id: file_item_id});
if (uploader2_xhr == null)
submit_one_file();
currentindex++;
}
}
function submit_one_file() {
var fileinfo = uploader2_files_queue.pop();
var file = fileinfo.file;
var uploaderid = fileinfo.uploaderid;
var tablename = fileinfo.tablename;
var tableid = fileinfo.tableid;
var file_item_id = fileinfo.file_item_id;
var file_item_selector = "#" + file_item_id;
jQuery(file_item_selector).addClass("upload2_started").removeClass("upload2_notstarted");
var file_item_progress_selector = "#" + file_item_id + "_progress";
uploader2_xhr = new XMLHttpRequest();
uploader2_xhr.upload.addEventListener("progress", function (e) {
var pc = Math.ceil(e.loaded / e.total * 100);
jQuery(file_item_progress_selector).text(pc + "%");
}, false);
uploader2_xhr.onreadystatechange = function (e) {
if (uploader2_xhr.readyState === 4) {
if (uploader2_xhr.status === 200) {
var result = jQuery.parseJSON(uploader2_xhr.responseText);
alertbox(result.message, result.type);
jQuery(file_item_selector).addClass("upload2_completed").removeClass("upload2_started");
uploader2_xhr = null;
if (uploader2_files_queue.length > 0)
submit_one_file();
refreshlist(uploaderid, result.data);
} else {
if (uploader2_xhr.status != 0) {
alertbox("خطا در ارسال فایل", 'error');
}
jQuery(file_item_selector).addClass("upload2_error").removeClass("upload2_started");
jQuery(file_item_selector).remove();
uploader2_xhr = null;
if (uploader2_files_queue.length > 0)
submit_one_file();
}
}
};
uploader2_xhr.open("POST", "/lib/class/uploader2/uploader2upload.php", true);
uploader2_xhr.setRequestHeader("X-File-Name", unescape(encodeURIComponent(file.name)));
uploader2_xhr.setRequestHeader("X-File-Size", file.size);
var formData = new FormData();
formData.append("file", file);
formData.append("tablename", tablename);
formData.append("id", tableid);
uploader2_xhr.send(formData);
}
// upload2_completed
function uploader_delete_item(file_item_id) {
var file_item_selector = "#" + file_item_id;
if (jQuery(file_item_selector).hasClass("upload2_error")) {
jQuery(file_item_selector).remove();
return;
}
if (jQuery(file_item_selector).hasClass("upload2_notstarted")) {
jQuery(file_item_selector).remove();
return;
}
if (jQuery(file_item_selector).hasClass("upload2_started")) {
if (uploader2_xhr !== null) {
uploader2_xhr.abort();
if (uploader2_files_queue.length > 0)
submit_one_file();
}
jQuery(file_item_selector).remove();
return;
}
if (jQuery(file_item_selector).hasClass("upload2_server")) {
file_item_id = file_item_id.substring(file_item_id.lastIndexOf("_") + 1);
jQuery.post("/lib/class/uploader2/uploader2delete.php", {"id": file_item_id}, function () {
jQuery(file_item_selector).remove();
alertbox("فایل حذف شد", 'ok');
});
}
}
function firstload(uploaderid, tablename, id) {
jQuery.post("/lib/class/uploader2/uploader2list.php", {"id": id, "tablename": tablename}, function (response) {
var result = jQuery.parseJSON(response);
refreshlist(uploaderid, result.data);
});
}
function refreshlist(uploaderid, data) {
var uploaderid_filelist = "#" + uploaderid + "_filelist";
var items = jQuery(uploaderid_filelist).children(".upload2_completed, .upload2_server");
var lastindexOfjson = 0;
var lastindexOfItems = 0;
while (data != null && lastindexOfjson < data.length && lastindexOfItems < items.length) {
var id_in_client = items.eq(lastindexOfItems).attr("id");
if (id_in_client.indexOf("server_") === 0) {
var id_in_server = "server_" + uploaderid + "_" + data[lastindexOfjson][0];
if (id_in_client === id_in_server) {
lastindexOfjson++;
lastindexOfItems++;
} else if (id_in_client > id_in_server) {
items.eq(lastindexOfItems).hide();
lastindexOfItems++;
} else {
var file_item_id = "server_" + uploaderid + "_" + data[lastindexOfjson][0];
var filename = data[lastindexOfjson][1];
var path = data[lastindexOfjson][2];
var filetype = data[lastindexOfjson][3];
var filesize = data[lastindexOfjson][4];
var item_html = get_file_item_html(file_item_id, filename, filesize, filetype, path);
items.eq(lastindexOfItems).before(item_html);
jQuery("#" + file_item_id).addClass("upload2_server");
lastindexOfjson++;
}
} else {
items.eq(lastindexOfItems).hide();
lastindexOfItems++;
}
}
if (data != null && lastindexOfjson < data.length) {
while (lastindexOfjson < data.length) {
var file_item_id = "server_" + uploaderid + "_" + data[lastindexOfjson][0];
var filename = data[lastindexOfjson][1];
var path = data[lastindexOfjson][2];
var filetype = data[lastindexOfjson][3];
var filesize = data[lastindexOfjson][4];
var item_html = get_file_item_html(file_item_id, filename, filesize, filetype, path);
jQuery(uploaderid_filelist).append(item_html);
jQuery("#" + file_item_id).addClass("upload2_server");
lastindexOfjson++;
}
} else {
while (lastindexOfItems < items.length) {
items.eq(lastindexOfItems).hide();
lastindexOfItems++;
}
}
items.filter(":hidden").remove();
}
function find_logo(fileType, filePath, filename) {
var prefix_icon_url = "/lib/class/uploader2/icons/";
var logo = prefix_icon_url + 'uploading.gif';
if (fileType) {
switch (fileType) {
case 'image/png':
case 'image/jpeg':
case 'image/gif':
var logo_filename = "thumb_w75_" + filename;
logo = filePath.split(filename).join(logo_filename);
if (existsFile(logo) == false) {
logo = filePath;
}
break;
case 'image/bmp':
logo = filePath;
break;
case 'application/msword':
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
logo = prefix_icon_url + "docx.png";
break;
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
case 'application/vnd.ms-excel:needs-lock=*':
logo = prefix_icon_url + "xlsx.png";
break;
case 'application/pdf':
logo = prefix_icon_url + "pdf.png";
break;
case 'application/vnd.ms-powerpoint':
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
logo = prefix_icon_url + "pptx.png";
break;
case 'application/zip':
case 'application/x-rar-compressed':
logo = prefix_icon_url + "zip.png";
break;
case 'video/mpeg':
case 'video/mp4':
case 'video/x-ms-wmv':
case 'video/quicktime':
logo = prefix_icon_url + "mpeg.png";
break;
case 'audio/wav':
case 'audio/mpeg':
case 'audio/x-ms-wma':
case 'audio/x-ms-wma':
case 'application/octet-stream': // for amr
logo = prefix_icon_url + "midi.png";
break;
// case 'undefined':
// logo = prefix_icon_url + 'uploading.gif';
// break;
default:
logo = prefix_icon_url + "unknown.png";
break;
}
}
var logostring = ''
return logostring;
}
function existsFile(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
if (http.status === 404) {
return false;
} else {
return true;
}
}