中扬CRM客户关系管理系统
LSH
2023-07-31 1bd2237b161e519c340052916c8e941a16595ba3
#售前规划申请单上传文件预览
4个文件已修改
139 ■■■■■ 已修改文件
src/main/java/com/zy/crm/common/utils/FileSaveExampleUtil.java 65 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/crm/manager/controller/PlanController.java 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/webapp/static/js/plan/plan.js 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/webapp/views/plan/plan.html 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/crm/common/utils/FileSaveExampleUtil.java
@@ -4,6 +4,10 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
public class FileSaveExampleUtil {
@@ -61,4 +65,65 @@
        File dest = new File(filePath);
        file.transferTo(dest);
    }
    public static void deleteFilesInDirectory(String directoryPath) {
        File directory = new File(directoryPath);
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        file.delete();
                        System.out.println("Deleted file: " + file.getAbsolutePath());
                    }
                }
            }
        }
    }
    public static List<FileDTO> viewFileList(String directoryPath) {
        List<FileDTO> fileList = new ArrayList<>();
        File directory = new File(directoryPath);
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        FileDTO fileDTO = new FileDTO(file.getName(), file.length(), file.getAbsolutePath());
//                        FileDTO fileDTO = new FileDTO(file.getName(), file.length());
                        fileList.add(fileDTO);
                    }
                }
            }
        }
        return fileList;
    }
    @Data
    public static class FileDTO {
        private String name;
        private long size;
        private String path;
        public FileDTO(String name, long size, String path) {
            this.name = name;
            this.size = size;
            this.path = path;
        }
        public FileDTO(String name, long size) {
            this.name = name;
            this.size = size;
//            this.path = path;
        }
        // getters and setters
    }
//    public static void main(String[] args) {
//        String directoryPath = "/path/to/directory";
//        deleteFilesInDirectory(directoryPath);
//    }
}
src/main/java/com/zy/crm/manager/controller/PlanController.java
@@ -226,6 +226,35 @@
        return R.ok();
    }
    @RequestMapping(value = "/plan/delete/file/auth")
//    @ManagerAuth
    public R deleteFile(@RequestParam("planId") String planId,@RequestParam MultipartFile file){
        planId="上传文件-20";
        String[] split = planId.split("-");
        int id = Integer.parseInt(split[1]);
        System.out.println("---开始---");
        String savePath = "D:/crm/plan/file/"+id+"/"+file.getName(); // 指定保存文件的路径
        try{
            FileSaveExampleUtil.deleteFilesInDirectory(savePath);
        }catch (Exception e){
            return R.error();
        }
        return R.ok();
    }
    @RequestMapping(value = "/plan/view/file/auth")
//    @ManagerAuth
    public R viewFile(@RequestParam("planId") String planId){
        int id = Integer.parseInt(planId);
        String savePath = "D:/crm/plan/file/"+id+"/"; // 指定保存文件的路径
        try{
            List<FileSaveExampleUtil.FileDTO> fileDTOS = FileSaveExampleUtil.viewFileList(savePath);
            return R.ok(fileDTOS);
        }catch (Exception e){
            return R.error();
        }
    }
    @PostMapping(value = "/plan/approval/auth")
src/main/webapp/static/js/plan/plan.js
@@ -204,7 +204,7 @@
        ,url: '/plan/insert/file/auth' //此处用的是第三方的 http 请求演示,实际使用时改成您自己的上传接口即可。
        ,accept: 'file'
        ,multiple: true
        ,number: 3
        ,number: 10
        ,auto: false
        ,bindAction: '#testListAction'
        ,choose: function(obj){
@@ -544,6 +544,47 @@
                    title: '上传文件-'+data.id,
                    content: $('#myModal')
                });
                // 获取路径下的文件列表,使用 jQuery 的 ajax 方法
                $.ajax({
                    url: '/plan/view/file/auth',
                    data:{
                        planId:data.id
                    },
                    success: function(response) {
                        if (response.code==200){
                            var targetTable = document.getElementById("data-btn-file3");
                            // var targetTable1 = document.getElementById("layui-layer2");
                            // // 获取文本内容
                            // var text = targetTable1.textContent.trim();
                            // console.log(text); // 输出 '上传文件-20'
                            targetTable.innerHTML = '';
                            // 将获取到的文件列表添加到文件队列中进行显示
                            response.data.forEach(function(file,index) {
                                console.log('Failed to get file list data.');
                                console.log(file);
                                // 创建tr元素
                                var tr = document.createElement("tr");
                                tr.id = "upload-"+index;
                                tr.innerHTML = '<td>' + file.name + '</td>'
                                    + '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                                    + '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                                    + '<td>'
                                    + '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                                    + '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                                    + '</td>';
                                // 将tr元素添加到目标table中
                                targetTable.appendChild(tr);
                            });
                        }else {
                            console.log('Failed to get file list error.');
                        }
                    },
                    error: function() {
                        console.log('Failed to get file list.');
                    }
                });
                break;
        }
    });
src/main/webapp/views/plan/plan.html
@@ -106,7 +106,7 @@
    </div>
</div>
<div id="myModal"  layui-hide="true">
<div id="myModal"  style="display: none;">
    <div style="padding: 10px">
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="data-btn-file2">选择文件</button><input class="layui-upload-file" type="file" accept="" name="file" multiple="">