mirror of
https://github.com/mattintech/simplefileupload-server.git
synced 2026-07-11 16:31:53 +00:00
Fix code input clearing behavior on download
Validate download code before starting download to ensure code input is only cleared on successful downloads. Invalid or expired codes now show an error message while preserving the entered code for correction.
This commit is contained in:
@@ -280,7 +280,7 @@
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const code = Array.from(inputs).map(input => input.value).join('');
|
||||
|
||||
|
||||
if (code.length !== 6) {
|
||||
showMessage('Please enter a valid 6-character code.', 'error');
|
||||
return;
|
||||
@@ -292,8 +292,26 @@
|
||||
downloadButton.textContent = 'Downloading...';
|
||||
|
||||
try {
|
||||
window.location.assign(`/download/${code}`);
|
||||
showMessage('Download started!', 'success');
|
||||
// First check if the code is valid by making a HEAD request
|
||||
const checkResponse = await fetch(`/download/${code}`, {
|
||||
method: 'HEAD'
|
||||
});
|
||||
|
||||
if (checkResponse.ok) {
|
||||
// Code is valid, start download and clear the input
|
||||
window.location.assign(`/download/${code}`);
|
||||
showMessage('Download started!', 'success');
|
||||
|
||||
// Clear the code inputs after successful download
|
||||
setTimeout(() => {
|
||||
form.reset();
|
||||
downloadButton.disabled = true;
|
||||
deleteButton.disabled = true;
|
||||
}, 500);
|
||||
} else {
|
||||
// Code is invalid, show error and don't clear
|
||||
showMessage('Invalid or expired code. Please check and try again.', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showMessage('Error starting download. Please try again.', 'error');
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user