'use strict';
/**
* 材料调差 - 附件
* @author LanJianRong
* @date 2020/06/30
* @version
*/
$(document).ready(function () {
$('#upload-file-ok').click(function () {
const files = Array.from($('#upload-fujian-file')[0].files)
const valiData = files.map(v => {
const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
return {
size: v.size,
ext
}
})
if (validateFiles(valiData)) {
if (files.length) {
const formData = new FormData()
files.forEach(file => {
formData.append('file', file)
formData.append('name', file.name)
formData.append('size', file.size)
})
postDataWithFile(window.location.pathname + '/upload', formData, function (result) {
$('#addfujian').modal('hide');
let html = '';
result.forEach((fileInfo, idx) => {
html += `
${idx + 1} |
${fileInfo.file_name} |
${fileInfo.file_size} |
${fileInfo.upload_time} | `
if (user_id == fileInfo.user_id && !checked) {
html += `
|
`
} else {
html += ` | `
}
})
$('#file-list').empty();
$('#file-list').append(html);
console.log(result)
});
}
}
})
$('#file-checkbox').click(function() {
let isCheck = false
if($(this).is(':checked')) {
isCheck = true
}
postData(window.location.pathname + '/find', {isCheck}, function(result) {
let html = '';
result.forEach((fileInfo, idx) => {
html += `
${idx + 1 } |
${fileInfo.file_name} |
${fileInfo.file_size} |
${fileInfo.upload_time} | `
if (user_id == fileInfo.user_id && !checked) {
html += `
|
`
} else {
html += ` | `
}
})
$('#file-list').empty();
$('#file-list').append(html);
})
})
// 删除附件
$('body').on('click', '.delete-file', function () {
let attid = $(this).data('attid');
let self = $(this);
const data = {id: attid};
postData('/tender/measure/material/file/delete', data, function (result) {
self.parents('tr').remove();
// 重新排序
let newsort = 1;
$('#file-list tr').each(function(){
$(this).children('td').eq(0).text(newsort);
newsort++;
});
});
});
});
/**
* 校验文件大小、格式
* @param {Array} files 文件数组
*/
function validateFiles(files) {
const reg = /(doc|docx|excel|xlsx|xls|txt|zip|jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
return files.every(file => {
if (file.size > 1024 * 1024 * 10) {
toastr.error('文件大小限制为10MB');
return false
}
if (!reg.test(file.ext)) {
toastr.error('请上传正确的格式文件');
return false
}
return true
})
}