53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
|
document.addEventListener("DOMContentLoaded", function() {
|
||
|
const input = document.getElementById("command-input");
|
||
|
const fileInput = document.getElementById("fileInput");
|
||
|
let isUploadTriggered = false;
|
||
|
|
||
|
document.querySelector("form").addEventListener("submit", function(event) {
|
||
|
const command = input.value.trim();
|
||
|
const parts = command.split(" ");
|
||
|
|
||
|
if (parts[0] === "upload") {
|
||
|
event.preventDefault();
|
||
|
if (parts.length === 1) {
|
||
|
fileInput.click();
|
||
|
isUploadTriggered = true;
|
||
|
} else {
|
||
|
const filePath = parts[1];
|
||
|
const targetPath = parts.length > 2 ? parts[2] : ".";
|
||
|
uploadFileFromBrowser(filePath, targetPath);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
fileInput.addEventListener("change", function () {
|
||
|
if (fileInput.files.length > 0 && isUploadTriggered) {
|
||
|
const file = fileInput.files[0];
|
||
|
input.value = 'upload "' + file.name + '"';
|
||
|
isUploadTriggered = false;
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
|
||
|
function uploadFileFromBrowser(filePath, targetPath) {
|
||
|
const fileInput = document.getElementById("fileInput");
|
||
|
if (fileInput.files.length === 0) {
|
||
|
console.error("No file selected");
|
||
|
alert("No file selected");
|
||
|
return;
|
||
|
}
|
||
|
const file = fileInput.files[0];
|
||
|
const formData = new FormData();
|
||
|
formData.append("file", file);
|
||
|
fetch("/upload", {
|
||
|
method: "POST",
|
||
|
body: formData,
|
||
|
})
|
||
|
.then((response) => response.text())
|
||
|
.then((data) => {
|
||
|
console.log("Upload successful:", data);
|
||
|
document.getElementById("command-input").value = "";
|
||
|
})
|
||
|
.catch((error) => console.error("Upload failed:", error));
|
||
|
}
|