123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- '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 += `<tr>
- <td>${idx + 1}</td>
- <td><a href="/${fileInfo.file_path}">${fileInfo.file_name}</a></td>
- <td>${fileInfo.file_size}</td>
- <td>${fileInfo.upload_time}</td>`
- if (user_id == fileInfo.user_id && !checked) {
- html += `<td>
- <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
- <span class="fa fa-trash text-danger"></span>
- </a>
- </td></tr>`
- } else {
- html += `<td></td></tr>`
- }
- })
- $('#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 += `<tr>
- <td>${idx + 1 }</td>
- <td><a href="/${fileInfo.file_path}">${fileInfo.file_name}</a></td>
- <td>${fileInfo.file_size}</td>
- <td>${fileInfo.upload_time}</td>`
- if (user_id == fileInfo.user_id && !checked) {
- html += `<td>
- <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
- <span class="fa fa-trash text-danger"></span>
- </a>
- </td></tr>`
- } else {
- html += `<td></td></tr>`
- }
- })
- $('#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
- })
- }
|