<input type="file"> 因安全性考量,value 屬性唯讀,無法直接賦值或清空。本文詳細說明獲取檔案資訊、預覽圖片、兩種清空方法的原理與實作。
<input type="file" id="fileInput" accept="image/*">
<div id="preview"></div>
<script>
const input = document.getElementById('fileInput');
const preview = document.getElementById('preview');
input.addEventListener('change', e => {
const file = e.target.files[0];
if (!file) return;
console.log('檔案名稱:', file.name);
console.log('檔案大小:', file.size, 'bytes');
console.log('MIME 類型:', file.type);
console.log('最後修改:', new Date(file.lastModified));
// 圖片預覽
if (file.type.startsWith('image/')) {
const reader = new FileReader();
reader.onload = e => {
preview.innerHTML = `
`;
};
reader.readAsDataURL(file);
}
});
</script>
function clearFileInput1(input) {
// 直接替換 outerHTML,瀏覽器會建立新元素
input.outerHTML = input.outerHTML.replace(/(value=")[^"]*"/i, '$1"');
// 注意:原本綁定在 input 上的事件監聽器會失效,需重新綁定
}
// 更簡潔的寫法
function clearFileInput1Simple(input) {
input.value = ''; // 現代瀏覽器(Chrome 57+, Firefox 57+, Edge 16+, Safari 11+)支援直接設為空字串
// 若不支援,改用:
// input.outerHTML = input.outerHTML;
}
function clearFileInput2(fileInput) {
// 1. 建立臨時 form
const form = document.createElement('form');
document.body.appendChild(form);
// 2. 記住原本位置
const nextSibling = fileInput.nextSibling;
const parent = fileInput.parentNode;
// 3. 移入 form 並 reset
form.appendChild(fileInput);
form.reset(); // 這會重置 form 內所有表單控制項
// 4. 移回原位置
parent.insertBefore(fileInput, nextSibling);
// 5. 清理
document.body.removeChild(form);
}
// 簡化版(若 input 已在 form 內)
function clearFileInputInForm(fileInput) {
const form = fileInput.form || fileInput.closest('form');
if (form) {
form.reset();
} else {
// 不在 form 內,用方法一
fileInput.value = '';
}
}
自 2017 年起,主流瀏覽器均支援直接設定 value = '':
// 現代標準寫法
document.getElementById('fileInput').value = '';
// 批次清空多個
document.querySelectorAll('input[type="file"]').forEach(input => input.value = '');
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>檔案上傳清空示範</title>
<style>
.file-group { margin: 10px 0; }
.preview img { max-width: 150px; max-height: 150px; border: 1px solid #ddd; }
</style>
</head>
<body>
<div class="file-group">
<input type="file" id="file1" accept="image/*">
<button type="button" onclick="clearFile('file1')">清空</button>
<div class="preview" id="preview1"></div>
</div>
<script>
// 綁定預覽
['file1'].forEach(id => {
const input = document.getElementById(id);
const preview = document.getElementById('preview' + id.slice(-1));
input.addEventListener('change', e => {
const file = e.target.files[0];
if (file && file.type.startsWith('image/')) {
const reader = new FileReader();
reader.onload = e => preview.innerHTML = `
`;
reader.readAsDataURL(file);
}
});
});
function clearFile(id) {
const input = document.getElementById(id);
const preview = document.getElementById('preview' + id.slice(-1));
// 現代瀏覽器直接清空
input.value = '';
preview.innerHTML = '';
// 兼容舊瀏覽器 fallback
if (input.value !== '') {
input.outerHTML = input.outerHTML;
// 重新綁定事件...
}
}
</script>
</body>
</html>
value 唯讀設計防止惡意網站竊取用戶本地檔案input.value = ''form.reset() 方法outerHTML 重建(會丟失事件綁定、焦點、驗證狀態)