From 1220369713aee4353ef0e1d5442299bb6d8f621f Mon Sep 17 00:00:00 2001 From: sbx0 Date: Thu, 11 Jan 2024 18:03:31 +0800 Subject: [PATCH] support upload by paste copied file --- .../sbx0/todo/scheduled/ServerScheduled.java | 8 ++-- .../src/main/resources/application-dev.yml | 4 ++ .../app/file/components/upload/upload.jsx | 48 +++++++++++-------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/todo-service/src/main/java/cn/sbx0/todo/scheduled/ServerScheduled.java b/todo-service/src/main/java/cn/sbx0/todo/scheduled/ServerScheduled.java index 908cd5c..ecd56f8 100644 --- a/todo-service/src/main/java/cn/sbx0/todo/scheduled/ServerScheduled.java +++ b/todo-service/src/main/java/cn/sbx0/todo/scheduled/ServerScheduled.java @@ -36,17 +36,17 @@ public class ServerScheduled { @Resource private TaskService taskService; - @Scheduled(fixedRate = 1000) + // @Scheduled(fixedRate = 1000) public void handleReminderTime() { taskService.handleReminderTime(); } - @Scheduled(fixedRate = 1000) + // @Scheduled(fixedRate = 1000) public void handleMessage() { chatGPTService.handleMessage(); } - @Scheduled(fixedRate = 1000) + // @Scheduled(fixedRate = 1000) public void sendMessage() { chatGPTService.sendMessage(); } @@ -54,7 +54,7 @@ public void sendMessage() { // 30min @Scheduled(fixedRate = 1800000) // 15min @Scheduled(fixedRate = 900000) // 1min @Scheduled(fixedRate = 60000) - @Scheduled(fixedRate = 3600000) + // @Scheduled(fixedRate = 3600000) public void getCarPlatePhoto() { DefaultPagingRequest pagingRequest = new DefaultPagingRequest(); pagingRequest.setPage(1); diff --git a/todo-service/src/main/resources/application-dev.yml b/todo-service/src/main/resources/application-dev.yml index 6408900..706ef99 100644 --- a/todo-service/src/main/resources/application-dev.yml +++ b/todo-service/src/main/resources/application-dev.yml @@ -19,6 +19,10 @@ spring: password: justfordev repositories: enabled: false + servlet: + multipart: + max-file-size: 10MB + max-request-size: 10MB logging: file: name: ./logs/current.log diff --git a/todo-web/app/file/components/upload/upload.jsx b/todo-web/app/file/components/upload/upload.jsx index 60fdd97..ffc6225 100644 --- a/todo-web/app/file/components/upload/upload.jsx +++ b/todo-web/app/file/components/upload/upload.jsx @@ -22,19 +22,10 @@ function DragAndDropUpload() { event.dataTransfer.dropEffect = 'copy'; }; - const handleDrop = (event) => { - event.preventDefault(); - setDragging(false); - - const uploadFiles = event.dataTransfer.files; - // 处理上传文件的逻辑,例如使用FormData发送到服务器 - + const handleUpload = (uploadFiles) => { console.log('Dropped files:', uploadFiles); - // 创建 FormData 对象 const formData = new FormData(); - - // 将每个文件添加到 FormData for (let i = 0; i < uploadFiles.length; i++) { formData.append('file', uploadFiles[i]); } @@ -49,25 +40,43 @@ function DragAndDropUpload() { } } - // 使用 fetch 发送文件到服务器 fetch('/api/file/upload', { method: 'POST', body: formData, headers: headers }) .then(response => response.json()) - .then(data => { - let newFiles = files.slice(0); - let file = data.data; - newFiles.push(file); - setFiles(newFiles); + .then(json => { + if (json.success) { + let newFiles = files.slice(0); + let file = json.data; + newFiles.push(file); + setFiles(newFiles); + } else { + console.error('Upload failed:', json.message); + } }) .catch(error => { - // 处理上传失败的逻辑 console.error('Upload failed:', error); }); + } + + const handleDrop = (event) => { + event.preventDefault(); + setDragging(false); + const uploadFiles = event.dataTransfer.files; + handleUpload(uploadFiles); }; + const handlePaste = (event) => { + event.preventDefault(); + const clipData = event.clipboardData || window.clipboardData; + const isFiles = !!clipData.files?.length; + if (isFiles) { + handleUpload(clipData.files); + } + } + return (
-

拖拽上传

+

拖拽/粘贴上传

- 拖拽文件到此区域进行上传 + 拖拽/粘贴文件到此区域进行上传

{dragging ? '放开以上传文件' : '拖拽文件到此区域'}