Skip to content

Commit 21bb46f

Browse files
committed
Fixxed MultipleUploadRequest Bug
1 parent c90b42c commit 21bb46f

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

src/main/java/de/jadenk/springcloud/controller/DashboardController.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.springframework.web.multipart.MultipartFile;
2727

2828
import java.security.Principal;
29+
import java.time.Instant;
2930
import java.util.*;
31+
import java.util.concurrent.ConcurrentHashMap;
3032
import java.util.stream.Collectors;
3133

3234
@Controller
@@ -140,6 +142,8 @@ public String dashboard(@RequestParam(value = "error", required = false) String
140142
// Fehlermeldungen beim Upload
141143
if ("uploadError".equals(error)) {
142144
model.addAttribute("error", "There was an Error while Uploading. Try again later.");
145+
} else if ("uploadInProgress".equals(error)) {
146+
model.addAttribute("error", "There is an current Upload in Progress.");
143147
} else if (error != null) {
144148
model.addAttribute("error", "An Error occurred.");
145149
}
@@ -203,24 +207,29 @@ public String deleteFolder(@RequestParam Long folderId, Principal principal) {
203207
public String handleUpload(@RequestParam("file") MultipartFile[] files,
204208
@RequestParam(required = false) Long folderId,
205209
Principal principal) {
206-
User currentUser = userRepository.findByUsername(principal.getName()).orElseThrow();
207210

208-
Folder folder = null;
209-
if (folderId != null) {
210-
folder = folderRepository.findById(folderId).orElseThrow();
211-
}
211+
String username = principal.getName();
212+
213+
try {
214+
// System.out.println("DashboardController >> L_214");
215+
User currentUser = userRepository.findByUsername(username).orElseThrow();
216+
Folder folder = null;
217+
if (folderId != null) {
218+
folder = folderRepository.findById(folderId).orElseThrow();
219+
}
212220

213-
for (MultipartFile file : files) {
214-
if (!file.isEmpty()) {
215-
try {
221+
for (MultipartFile file : files) {
222+
if (!file.isEmpty()) {
216223
fileUploadService.uploadFile(file, currentUser, folder);
217-
} catch (Exception e) {
218-
e.printStackTrace();
219-
return "redirect:/dashboard?error=uploadError";
220224
}
221225
}
226+
} catch (Exception e) {
227+
e.printStackTrace();
228+
return "redirect:/dashboard?error=uploadError";
222229
}
223-
return "redirect:/dashboard" + (folderId != null ? "?folderId=" + folderId : "");
230+
231+
return "redirect:/dashboard";
232+
//return "redirect:/dashboard" + (folderId != null ? "?folderId=" + folderId : "");
224233
}
225234

226235
// =========================

src/main/java/de/jadenk/springcloud/service/FileUploadProgressListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void updateProgress(long bytesRead) {
2020
this.bytesRead = bytesRead;
2121
this.progress = (double) bytesRead / totalBytes * 100;
2222

23-
// System.out.println("Uploading Progress: " + (bytesRead * 100 / totalBytes) + "%");
23+
// System.out.println("Uploading Progress: " + (bytesRead * 100 / totalBytes) + "%");
2424
}
2525

2626
public double getProgress() {

src/main/resources/static/js/dashboard.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,5 @@ function closeDeleteFolderPopup() {
217217
document.getElementById('deleteFolderPopup').classList.add('hidden');
218218
}
219219

220+
220221
showPage(1);

src/main/resources/templates/dashboard.html

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ <h2 class="text-xl font-bold text-[var(--color-primary)]">Really delete this fol
266266
<div id="uploadPopup" class="hidden popup-overlay">
267267
<div class="popup-container">
268268
<h2 class="text-xl font-bold text-[var(--color-primary)]">Upload Your Files</h2>
269-
<form id="uploadForm" th:action="@{/upload}" method="post" enctype="multipart/form-data" class="flex flex-col gap-4" onsubmit="showLoadingBar()">
269+
<form id="uploadForm" th:action="@{/upload}" method="post" enctype="multipart/form-data" class="flex flex-col gap-4">
270270
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
271271
<div id="dropArea" class="border-2 border-dashed border-gray-600 rounded-xl p-6 text-center cursor-pointer">
272272
Drag & Drop files here or click to select
@@ -314,20 +314,9 @@ <h2 class="text-xl font-bold text-[var(--color-primary)]">Upload Your Files</h2>
314314
}
315315

316316
document.getElementById('uploadForm').addEventListener('submit', function(e){
317-
if(selectedFiles.length > 0){
318-
const formData = new FormData(this);
319-
selectedFiles.forEach(file => formData.append('file', file));
320-
321-
fetch(this.action, {
322-
method: this.method,
323-
body: formData
324-
}).then(res => {
325-
location.reload();
326-
}).catch(err => console.error(err));
327-
328-
e.preventDefault();
329-
}
317+
closeUploadPopup(); // nach fetch aufrufen
330318
});
319+
331320
</script>
332321
<div>
333322
<label for="folderSelect" class="block text-[var(--color-text)] font-semibold mb-2">Upload to folder</label>

0 commit comments

Comments
 (0)