var fileLists = [];
var fileNameList = []
// 选择文件
$("#selectFile").on('change', function (event) {
let _files = this.files;
// console.log(_files[0])
_files = Array.from(_files); //将伪数组专为真数组修改
for (let i = 0; i < _files.length; i++) {
// 文件去重处理
if (fileNameList.indexOf(_files[i].name) === -1) {
fileLists.push(_files[i]);
fileNameList.push(_files[i].name)
// let html = "<p class='file-name'>" + _files[i].name + sizeToStr(_files[i].size) + " <span class='icon-remove'><i class='fa fa-trash'></i></span></p>"
let html = "<p class='file-name'>" + _files[i].name + sizeToStr(_files[i].size) + getDeleteFileIcon(_files[i].id)
$('.upfile-list-mes').append(html);
}
}
})
$(function () {
var _files = MyViewVar.initialPreviewData
for (let i = 0; i < _files.length; i++) {
// 文件去重处理
if (fileNameList.indexOf(_files[i].name) === -1) {
addFile2FileLists(_files[i])
}
}
console.log(fileLists)
})
function addFile2FileLists(_files) {
var blob = null;
var xhr = new XMLHttpRequest();
xhr.open("GET", _files.url);
xhr.setRequestHeader("Content-type", "charset=utf-8");
xhr.responseType = "blob";
// 加载时处理
xhr.onload = () => {
// 获取返回结果
blob = xhr.response;
let file = new File([blob], _files.name, {type: _files.type});
// console.log('file==', file)
fileLists.push(file);
fileNameList.push(_files.name)
let html = "<p class='file-name'>" + _files.name + sizeToStr(_files.size) + downloadFile(_files.id) + getDeleteFileIcon(_files.id)
$('.upfile-list-mes').append(html);
};
// 发送
xhr.send();
}
function downloadFile(file_id) {
// console.log(file_id)
return " <a class='fa fa-download' href='/file/teams_data_structure_file_download/" + file_id + "'></a>"
}
function getDeleteFileIcon(file_id) {
return " <span οnclick='deleteFile(" + file_id + ", this)' class='icon-remove'><i class='fa fa-trash'></i></span></p>"
}
function deleteFile(file_id, e) {
console.log(file_id)
if (file_id) {
$.messager.confirm({
title: '提示', msg: '移除文件将会同步服务器删除文件!', fn: function (r) {
if (r) {
$.ajax({
url: server_url + '/file/del_teams_file_by_id/' + file_id + '/',
method: 'GET',
processData: false,
contentType: false,
cache: false,
success: function (data) {
console.log("data:" + data);
console.log("data:" + data.status);
if (data.status === 200) {
let ind = $(e).parent().index();
console.log(fileNameList[ind])
$(e).parent().remove();
fileLists.splice(ind, 1);
fileNameList.splice(ind, 1);
console.log(fileLists)
console.log("data:" + data.msg);
$.messager.alert({title: '提示', msg: data.msg, icon: 'info'});
return
}
console.log(data)
$.messager.alert({title: '提示', msg: data.msg, icon: 'warning'});
},
//请求失败,包含具体的错误信息
error: function (data) {
console.log('error' + data.msg);
$.messager.alert({title: '提示', msg: '请求服务错误或当前网络不佳!', icon: 'warning', top: 200});
}
});
}
}
});
} else {
let ind = $(e).parent().index();
console.log(fileNameList[ind])
$(e).parent().remove();
fileLists.splice(ind, 1);
fileNameList.splice(ind, 1);
console.log(fileLists)
}
}
function sizeToStr(size) {
var data = "";
if (size < 0.1 * 1024) { //如果小于0.1KB转化成B
data = size.toFixed(2) + "B";
} else if (size < 0.1 * 1024 * 1024) { //如果小于0.1MB转化成KB
data = (size / 1024).toFixed(2) + "KB";
} else if (size < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB
data = (size / (1024 * 1024)).toFixed(2) + "MB";
} else { //其他转化成GB
data = (size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
}
var sizestr = data + "";
var len = sizestr.indexOf("\.");
var dec = sizestr.substr(len + 1, 2);
if (dec === "00") { //当小数点后为00时 去掉小数部分
return ' (' + sizestr.substring(0, len) + sizestr.substr(len + 3, 2) + ')';
}
return ' (' + sizestr + ')';
}
// 删除文件
// $(document).on('click', '.icon-remove', function (event) {
// let ind = $(this).parent().index();
// $(this).parent().remove();
// fileLists.splice(ind, 1);
// fileNameList.splice(ind, 1);
// console.log(fileLists)
// });
function SubmitEditForm() {
var submitbtn1 = document.getElementById("SubmitTop");
var submitbtn2 = document.getElementById("SubmitBottom");
var title = document.getElementById("id_title").value;
var full_name = document.getElementById("id_full_name").value;
var knowledge_category = document.getElementById("id_knowledge_category").value;
var team = document.getElementById("id_team").value;
console.log(title, full_name, knowledge_category, team)
//确认必选项是否都已填
if (title === '' || full_name === '' || knowledge_category === '' || team === '') {
submitbtn2.disabled = false;
submitbtn1.disabled = false;
$.messager.alert("提示", 'Must input all items with * !', "warning");
return false;
}
var formData = new FormData($("#dataStructureForm")[0]);
// $("#uploadFileId").fileinput("upload"); // 单独上传文件接口
if (fileLists.length > 0) {
var fileSize = 0
// console.log(fileLists)
for (let i = 0; i < fileLists.length; i++) {
fileSize += fileLists[i].size
// console.log(sizeToStr(fileSize))
if (fileSize > 50 * 1024 * 1024) {
console.log('file size:', sizeToStr(fileSize))
$.messager.alert("提示", '所有文件大小不能超过50M!', "warning");
return
}
}
$(fileLists).each(function (i, e) {
console.log(e)
formData.append('uploadFile', e)
})
}
console.log(fileLists)
submit(formData)
}
function submit(formData) {
// var form = document.forms[0];
console.log(formData.data)
// var jsonData = JSON.stringify(formData);
var submitbtn1 = document.getElementById("SubmitTop");
var submitbtn2 = document.getElementById("SubmitBottom");
submitbtn2.disabled = true;
submitbtn1.disabled = true;
submitbtn1.value = "loading...";
submitbtn2.value = "loading...";
var id = $('#obj_id').val()
console.log(id)
$.ajax({
url: server_url + '/teams/data_structure_detail/' + id + '/',
method: 'PUT',
data: formData,
dataType: "json",
processData: false,
contentType: false,
cache: false,
success: function (data) {
console.log("data:" + data);
console.log("data:" + data.res);
if (data.status === 200) {
$.messager.alert("提示", data.msg, "info");
console.log("data:" + data.msg);
window.setTimeout("window.location=server_url+'/teams/data_structure'", 500);
return;
}
submitbtn2.disabled = false;
submitbtn1.disabled = false;
submitbtn1.value = "Submit";
submitbtn2.value = "Submit";
console.log(data)
$.messager.alert("提示", data.msg, "info");
},
//请求失败,包含具体的错误信息
error: function (data) {
console.log(data.msg);
}
});
}