html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Universal File Converter</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f0f2f5;
margin: 0;
padding: 0;
display: flex;
min-height: 100vh;
flex-direction: column;
align-items: center;
justify-content: flex-start;
padding: 40px 20px;
color: #333;
}
h1 {
margin-bottom: 10px;
}
.converter-container {
background: white;
padding: 30px 40px;
border-radius: 10px;
box-shadow: 0 6px 20px rgba(0,0,0,0.1);
max-width: 500px;
width: 100%;
}
label {
display: block;
margin-bottom: 6px;
font-weight: bold;
}
input[type="file"] {
width: 100%;
}
select {
width: 100%;
padding: 8px 12px;
margin-top: 5px;
margin-bottom: 20px;
font-size: 1rem;
border-radius: 5px;
border: 1px solid #ccc;
appearance: none;
background: url('data:image/svg+xml;utf8,<svg fill="gray" height="16" viewBox="0 0 24 24" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/></svg>') no-repeat right 12px center/12px 12px;
background-color: white;
}
button {
background-color: #007bff;
border: none;
color: white;
font-weight: bold;
padding: 12px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.25s ease;
width: 100%;
font-size: 1rem;
}
button:hover:enabled {
background-color: #0056b3;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.loading-bar-container {
width: 100%;
background-color: #e0e0e0;
border-radius: 10px;
overflow: hidden;
height: 20px;
margin: 20px 0;
display: none;
}
.loading-bar {
height: 100%;
width: 0%;
background-color: #007bff;
transition: width 0.2s linear;
}
.file-result-list {
margin-top: 25px;
list-style: none;
padding: 0;
}
.file-result-list li {
margin-bottom: 15px;
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid #ddd;
background: #fafafa;
padding: 10px 15px;
border-radius: 6px;
word-break: break-word;
}
.file-result-list li button {
background-color: #28a745;
padding: 6px 14px;
font-size: 0.9rem;
border-radius: 4px;
width: auto;
min-width: 80px;
}
.file-result-list li button:hover {
background-color: #1e7e34;
}
.footer {
margin-top: auto;
padding: 15px;
font-size: 0.9rem;
color: #999;
text-align: center;
}
</style>
</head>
<body>
<h1>Universal File Converter</h1>
<div class="converter-container" role="main" aria-label="File Converter">
<form id="converter-form">
<label for="file-input">Upload file(s)</label>
<input type="file" id="file-input" name="files" multiple aria-describedby="file-desc" required />
<div id="file-desc" style="font-size: 0.9rem; color: #666; margin-bottom: 15px;">
Select one or multiple files to convert.
</div>
<label for="format-select">Convert to format</label>
<select id="format-select" name="format" required aria-describedby="format-desc">
<option value="" disabled selected>Select format</option>
<optgroup label="Video">
<option value="mp4">MP4</option>
<option value="avi">AVI</option>
<option value="mov">MOV</option>
<option value="mkv">MKV</option>
</optgroup>
<optgroup label="Audio">
<option value="mp3">MP3</option>
<option value="wav">WAV</option>
<option value="aac">AAC</option>
<option value="flac">FLAC</option>
</optgroup>
<optgroup label="Image">
<option value="jpg">JPG</option>
<option value="png">PNG</option>
<option value="gif">GIF</option>
<option value="bmp">BMP</option>
<option value="webp">WEBP</option>
</optgroup>
<optgroup label="Documents">
<option value="pdf">PDF</option>
<option value="txt">TXT</option>
<option value="docx">DOCX</option>
<option value="xlsx">XLSX</option>
</optgroup>
</select>
<button type="submit" id="convert-btn" aria-live="polite">Convert</button>
<div class="loading-bar-container" aria-hidden="true" aria-live="assertive" aria-atomic="true" aria-label="Uploading progress">
<div class="loading-bar" id="loading-bar"></div>
</div>
</form>
<ul class="file-result-list" id="result-list" aria-live="polite" aria-relevant="additions"></ul>
</div>
<div class="footer">
© 2024 Universal File Converter. This is a demo; actual file conversion is simulated.
</div>
<script>
(() => {
const form = document.getElementById('converter-form');
const fileInput = document.getElementById('file-input');
const formatSelect = document.getElementById('format-select');
const convertBtn = document.getElementById('convert-btn');
const loadingBarContainer = document.querySelector('.loading-bar-container');
const loadingBar = document.getElementById('loading-bar');
const resultList = document.getElementById('result-list');
// Because real file conversion requires backend or complex libs,
// this simulates the upload and conversion process with a 10s timer.
// Result will be a dummy link to download converted files (simulated).
function resetLoadingBar() {
loadingBar.style.width = '0%';
}
function startUploadProgress(duration = 10000) {
loadingBarContainer.style.display = 'block';
let start = null;
return new Promise((resolve) => {
function step(timestamp) {
if (!start) start = timestamp;
const elapsed = timestamp - start;
const progress = Math.min((elapsed / duration) * 100, 100);
loadingBar.style.width = progress + '%';
if (elapsed < duration) {
window.requestAnimationFrame(step);
} else {
resolve();
}
}
window.requestAnimationFrame(step);
});
}
// Generate a simulated converted file blob and download link:
// Use zip or just simulate separate files? We'll simulate files with dummy data indicating the new format.
// Because extracting actual file content or converting is not possible purely client-side without libraries.
// We'll name the files as originalName + "_converted." + newExtension
function generateDummyConvertedFile(originalFile, targetExt) {
// Generate blob data with text describing the converted file
const content = `Simulated converted file\nOriginal filename: ${originalFile.name}\nConverted to: .${targetExt}\nDate: ${new Date().toLocaleString()}`;
const blob = new Blob([content], { type: 'text/plain' });
const newFileName = originalFile.name.replace(/\.[^/.]+$/, '') + `_converted.${targetExt}`;
return { blob, name: newFileName };
}
// Create download link element for one converted file
function createDownloadLink(blob, name) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = name;
a.textContent = 'Download';
a.setAttribute('role', 'button');
a.classList.add('download-link');
// Cleanup URL after download link is removed (or page unloaded)
a.addEventListener('click', () => {
setTimeout(() => URL.revokeObjectURL(url), 1000);
});
return a;
}
function clearResults() {
resultList.innerHTML = '';
}
form.addEventListener('submit', async (e) => {
e.preventDefault();
const files = fileInput.files;
const targetFormat = formatSelect.value;
if (!files.length) {
alert('Please select at least one file.');
return;
}
if (!targetFormat) {
alert('Please select a target format.');
return;
}
convertBtn.disabled = true;
clearResults();
resetLoadingBar();
// Show loading bar and simulate upload/conversion delay
await startUploadProgress(10000);
// After "upload", simulate conversion and display download links
for (const file of files) {
const { blob, name } = generateDummyConvertedFile(file, targetFormat);
const li = document.createElement('li');
const fileNameSpan = document.createElement('span');
fileNameSpan.textContent = name;
li.appendChild(fileNameSpan);
const downloadBtn = createDownloadLink(blob, name);
li.appendChild(downloadBtn);
resultList.appendChild(li);
}
loadingBarContainer.style.display = 'none';
convertBtn.disabled = false;
// Reset form inputs (optional)
//form.reset();
});
})();
</script>
</body>
</html>