原文使用 ActiveXObject("Scripting.FileSystemObject"),**僅在 IE (含 IE 模式) 中於本地檔案/信任站點啟用 ActiveX 時可用**。現代瀏覽器 (Chrome, Firefox, Edge, Safari) **完全不支援**,且極具安全風險 (需降低安全性設定)。**生產環境嚴禁使用。**
<script>
function readFile(filename) {
const fso = new ActiveXObject("Scripting.FileSystemObject");
const f = fso.OpenTextFile(filename, 1); // 1 = ForReading
let s = "";
while (!f.AtEndOfStream) s += f.ReadLine() + "\n";
f.Close();
return s;
}
function writeFile(filename, content) {
const fso = new ActiveXObject("Scripting.FileSystemObject");
const f = fso.OpenTextFile(filename, 8, true); // 8 = ForAppending, true = create
f.WriteLine(content);
f.Close();
alert('ok');
}
</script>
<input type="text" id="in">
<button onclick="writeFile('c:/12.txt', document.getElementById('in').value)">寫入</button>
<button onclick="document.getElementById('show').value = readFile('c:/12.txt')">讀取</button>
<textarea id="show" cols="50" rows="8"></textarea>
<input type="file" id="fileInput" accept=".txt,.json,.csv">
<textarea id="content" rows="10" cols="60" readonly></textarea>
<script>
document.getElementById("fileInput").addEventListener("change", async (e) => {
const file = e.target.files[0];
if (!file) return;
const text = await file.text(); // File API: 非同步讀取文字
document.getElementById("content").value = text;
});
function saveFile(filename, content) {
const blob = new Blob([content], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url); // 釋放記憶體
}
<button onclick="saveFile('note.txt', document.getElementById('content').value)">下載儲存</button>
需使用者透過檔案選擇器授權,獲得 FileSystemHandle 後可隨時讀寫,**無需每次選檔**。
let fileHandle = null;
async function openFile() {
[fileHandle] = await window.showOpenFilePicker({ types: [{ description: 'Text', accept: { 'text/plain': ['.txt'] } }] });
const file = await fileHandle.getFile();
document.getElementById("content").value = await file.text();
}
async function saveFile() {
if (!fileHandle) {
[fileHandle] = await window.showSaveFilePicker({ suggestedName: 'note.txt', types: [{ description: 'Text', accept: { 'text/plain': ['.txt'] } }] });
}
const writable = await fileHandle.createWritable();
await writable.write(document.getElementById("content").value);
await writable.close();
}
<button onclick="openFile()">開啟本地檔案</button>
<button onclick="saveFile()">儲存回原檔案</button>
// CommonJS
const fs = require("fs/promises");
async function read(file) { return await fs.readFile(file, "utf8"); }
async function write(file, content) { await fs.writeFile(file, content, "utf8"); }
// 同步版 (僅建構腳本/CLI 使用)
const fsSync = require("fs");
fsSync.writeFileSync("a.txt", "hello");
console.log(fsSync.readFileSync("a.txt", "utf8"));
主進程擁有完整 Node.js `fs` 權限,渲染進程透過 IPC (preload 腳本) 安全暴露檔案操作給前端:
// preload.js (Context Isolation)
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("fs", {
read: (path) => ipcRenderer.invoke("fs:read", path),
write: (path, content) => ipcRenderer.invoke("fs:write", path, content)
});
// 前端使用
const text = await window.fs.read("C:/data/config.json");
| 場景 | 推薦方案 | 備註 |
|---|---|---|
| Web 頁面讀取使用者上傳檔案 | <input type=file> + file.text() | 標準、安全、全支援 |
| Web 頁面讓使用者下載儲存 | Blob + a[download] | 標準、不需權限 |
| Web App 需持續讀寫特定本地檔 | File System Access API | Chrome/Edge 支援,需使用者授權 |
| 內網舊系統 僅 IE/IE Mode | ActiveXObject (HTA/信任站點) | **技術債務,規劃遷移** |
| Node.js 腳本/伺服器/建構工具 | fs/promises | 標準非同步 API |
| Electron/Tauri 桌面應用 | 主進程 fs + Preload IPC | 安全邊界隔離 |
原文的 ActiveX 方案屬於 **IE 時代遺物**,現代 Web 開發請完全轉向 **File API / File System Access API / Node.js fs / 桌面應用 IPC**。理解歷史包袱有助於維護舊系統,但新專案務必採用標準、安全、跨平台的現代 API。