hey
- User
hey
- Assistant
Hello! How can I assist you today?
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
hey
Hello! How can I assist you today?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
// script.js const uploadLink = document.getElementById('uploadLink'); const uploadModal = document.getElementById('uploadModal'); const closeUploadModal = document.getElementsByClassName('close')[0]; const logoutLink = document.getElementById('logoutLink'); const form = document.getElementById('uploadForm'); const fileInput = document.getElementById('fileInput'); const fileList = document.getElementById('fileList').querySelector('ul');
const searchLink = document.getElementById('searchLink'); const searchModal = document.getElementById('searchModal'); const closeSearchModal = document.getElementsByClassName('close')[1]; const searchForm = document.getElementById('searchForm'); const searchInput = document.getElementById('searchInput'); const searchResults = document.getElementById('searchResults');
// Abrir modal para subir archivos uploadLink.addEventListener('click', () => { uploadModal.style.display = 'block'; });
// Cerrar modal de subida closeUploadModal.addEventListener('click', () => { uploadModal.style.display = 'none'; });
// Abrir modal de búsqueda searchLink.addEventListener('click', () => { searchModal.style.display = 'block'; });
// Cerrar modal de búsqueda closeSearchModal.addEventListener('click', () => { searchModal.style.display = 'none'; });
// Cerrar sesión (simplemente recarga la página) logoutLink.addEventListener('click', () => { window.location.reload(); });
// Subir archivo form.addEventListener('submit', async (e) => { e.preventDefault();
const formData = new FormData(); formData.append('file', fileInput.files[0]);
try { const response = await fetch('/upload', { method: 'POST', body: formData, });
if (response.ok) {
const message = await response.text();
console.log(message);
displayFiles();
uploadModal.style.display = 'none';
fileInput.value = ''; // Limpiar el campo de archivo
} else {
console.error('Error al subir el archivo');
}
} catch (error) { console.error('Error:', error); } });
// Buscar archivos searchForm.addEventListener('submit', async (e) => { e.preventDefault();
const query = searchInput.value.trim();
if (query === '') { return; }
try {
const response = await fetch(/search?q=${query});
const files = await response.json();
displaySearchResults(files);
} catch (error) { console.error('Error:', error); } });
// Obtener y mostrar la lista de archivos
function displayFiles() {
fetch('/uploads')
.then((response) => response.json())
.then((files) => {
fileList.innerHTML = '';
files.forEach((file) => {
const li = document.createElement('li');
const link = document.createElement('a');
link.href = /download/${file};
link.textContent = file;
li.appendChild(link);
const actions = document.createElement('div');
actions.classList.add('actions');
const deleteLink = document.createElement('a');
deleteLink.href = '#';
deleteLink.innerHTML = '<i class="fas fa-trash-alt"></i>';
deleteLink.addEventListener('click', () => deleteFile(file));
actions.appendChild(deleteLink);
const infoLink = document.createElement('a');
infoLink.href = '#';
infoLink.innerHTML = '<i class="fas fa-info-circle"></i>';
infoLink.addEventListener('click', () => showFileInfo(file));
actions.appendChild(infoLink);
li.appendChild(actions);
fileList.appendChild(li);
});
})
.catch((error) => console.error('Error:', error));
}
// Mostrar resultados de búsqueda function displaySearchResults(files) { searchResults.innerHTML = '';
if (files.length === 0) {
const p = document.createElement('p');
p.textContent = 'No se encontraron archivos.';
searchResults.appendChild(p);
} else {
const ul = document.createElement('ul');
files.forEach((file) => {
const li = document.createElement('li');
const link = document.createElement('a');
link.href = /download/${file};
link.textContent = file;
li.appendChild(link);
ul.appendChild(li);
});
searchResults.appendChild(ul);
}
}
// Eliminar un archivo
function deleteFile(filename) {
if (confirm(¿Estás seguro de que deseas eliminar ${filename}?)) {
fetch(/delete/${filename}, {
method: 'DELETE',
})
.then(() => {
displayFiles();
})
.catch((error) => console.error('Error:', error));
}
}
// Mostrar información de un archivo
function showFileInfo(filename) {
fetch(/file-info/${filename})
.then((response) => response.json())
.then((fileInfo) => {
const modal = document.createElement('div');
modal.classList.add('modal');
modal.innerHTML = <div class="modal-content"> <span class="close">×</span> <h2>Información del archivo</h2> <p><strong>Nombre:</strong> ${fileInfo.name}</p> <p><strong>Tamaño:</strong> ${formatFileSize(fileInfo.size)}</p> <p><strong>Fecha de creación:</strong> ${fileInfo.createdAt}</p> <p><strong>Última modificación:</strong> ${fileInfo.modifiedAt}</p> </div> ;
const closeModal = modal.querySelector('.close');
closeModal.addEventListener('click', () => {
modal.style.display = 'none';
document.body.removeChild(modal);
});
document.body.appendChild(modal);
modal.style.display = 'block';
})
.catch((error) => console.error('Error:', error));
}
// Formatear el tamaño de archivo
function formatFileSize(size) {
if (size < 1024) {
return ${size} bytes;
} else if (size < 1024 * 1024) {
return ${(size / 1024).toFixed(2)} KB;
} else if (size < 1024 * 1024 * 1024) {
return ${(size / (1024 * 1024)).toFixed(2)} MB;
} else {
return ${(size / (1024 * 1024 * 1024)).toFixed(2)} GB;
}
}
// Mostrar la lista de archivos al cargar la página displayFiles();
You've shared a comprehensive JavaScript code for managing a web interface related to file management, including features to upload, search, and delete files, as well as displaying file information through modals. This script is a good starting point for handling such functionality on a website. It leverages Fetch API for server interactions and DOM manipulation for dynamically updating the user interface based on user interactions and server responses.
The script includes the following functionalities:
Uploading Files: Users can upload files through a modal window. The form data is sent to a /upload endpoint using the Fetch API.
Listing Files: The script requests a list of uploaded files from an /uploads endpoint and dynamically updates the HTML list to display these files, including features to download, delete, or view more information about each file.
Searching Files: Users can search for files through a search modal. The search query is sent to a /search endpoint, and the search results are displayed dynamically on the page.
Deleting Files: Files can be deleted by clicking a delete icon next to each file. This action calls a /delete/{filename} endpoint using the Fetch API with the DELETE method.
Viewing File Information: Users can view detailed information about a file in a modal window. This feature fetches data from a /file-info/{filename} endpoint and displays it in a dynamically created modal.
Formatting File Size: A utility function formatFileSize is included to format the file size in a human-readable format (bytes, KB, MB, GB), improving the UX by making file sizes easy to interpret.
Modal Handling: The code handles opening and closing of modal windows for uploading files, searching files, and viewing file information, improving the user experience through interactive and modern UI components.
Key Takeaways for Improvements or Considerations:
Overall, your script serves as a solid foundation for a file management system in a web application. With additional considerations for security, accessibility, and responsive design, you can enhance this script to be more robust and user-friendly.
Not yet included in a published release.