File Uploads
HTML Living Standard
File picker နဲ့ user က ဖိုင်ရွေးနိုင်ပါတယ်။ ရွေးထားတဲ့ဖိုင်ကို upload လုပ်ဖို့ File picker ကို form ထဲ ထည့်ပြီး ဖိုင်လက်ခံပေးမယ့် server ဆီ ပို့ရပါတယ်။
Code စမ်းလို့ရတဲ့ သင်ခန်းစာ ဖွင့်ရန်
ဒီစာမျက်နှာမှာ
multipart/form-data နဲ့ ဖိုင်ပို့ခြင်း
action="/upload" က form ပို့မယ့် server route ကို သတ်မှတ်ပါတယ်။ method="post" က form data ကို request body ထဲ ထည့်ပို့ပါတယ်။ enctype="multipart/form-data" က form field တစ်ခုစီကို သီးခြားအပိုင်းအဖြစ် ပို့စေပြီး ရွေးထားတဲ့ဖိုင်ရဲ့ အကြောင်းအရာပါ ထည့်ပို့ပါတယ်။ Input ရဲ့ name="attachment" က server မှာ အဲဒီဖိုင်အပိုင်းကို ပြန်ရှာဖို့ သုံးတဲ့ နာမည်ပါ။
စမ်းသပ်နည်း ဒီနမူနာကို .html ဖိုင်အသစ်အဖြစ် သိမ်းပြီး browser နဲ့ ဖွင့်ပါ။ ပြင်ပြီးတိုင်း save နဲ့ reload လုပ်ပါ။
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Submit a File with multipart/form-data</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="attachment">Choose a text file</label>
<input id="attachment" name="attachment" type="file" accept=".txt,text/plain" required>
<button type="submit">Upload file</button>
</form>
</body>
</html>
စမ်းကြည့်ရင် ဘာဖြစ်မလဲ စာသားဖိုင်သေးသေးတစ်ဖိုင် ရွေးပြီး Upload file ကို နှိပ်ပါ။ Preview မှာ browser ထဲက ရွေးထားတဲ့ဖိုင်ကို သုံးပြီး method၊ destination၊ encoding၊ ဖိုင်နာမည်နဲ့ အရွယ်အစားကို ပြပါမယ်။ Local server ဆီ ဖိုင်တကယ်ပို့ဖို့ နောက်နမူနာအတိုင်း ဆက်စမ်းပါ။
Local Server မှာ ဖိုင်လက်ခံခြင်း
အပေါ်က HTML နမူနာကို upload.html အဖြစ် သိမ်းပါ။ အောက်က server code ကို folder တူတူထဲမှာ server.mjs အဖြစ် သိမ်းပါ။ Node.js 22 သို့မဟုတ် နောက်ပိုင်း version နဲ့ node server.mjs လို့ run ပြီး http://127.0.0.1:3000 ကို ဖွင့်ပါ။ စာသားပါတဲ့ .txt ဖိုင်တစ်ခု ရွေးပြီး submit လုပ်ပါ။ Server က attachment အပိုင်းကို ဖတ်ပြီး ရလာတဲ့ ဖိုင်နာမည်နဲ့ byte အရေအတွက်ကို ပြန်ပို့ပါတယ်။ ဒီ local နမူနာက request တစ်ခုလုံးကို 1 MiB အထိ လက်ခံပြီး ဖိုင်ကို ဖတ်ပြီးတာနဲ့ စွန့်ပစ်ပါတယ်။
import { readFile } from "node:fs/promises";
import { createServer } from "node:http";
const page = await readFile(new URL("./upload.html", import.meta.url));
const maxRequestBytes = 1024 * 1024;
function send(response, status, body) {
response.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
response.end(JSON.stringify(body));
}
createServer(async (request, response) => {
const path = request.url;
if (request.method === "GET" && path === "/") {
response.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
response.end(page);
return;
}
if (request.method !== "POST" || path !== "/upload") {
send(response, 404, { error: "Page not found" });
return;
}
const contentType = request.headers["content-type"] ?? "";
if (!contentType.toLowerCase().startsWith("multipart/form-data;")) {
send(response, 415, { error: "Send a multipart form" });
return;
}
try {
const chunks = [];
let size = 0;
for await (const chunk of request.iterator({ destroyOnReturn: false })) {
size += chunk.length;
if (size > maxRequestBytes) {
response.setHeader("Connection", "close");
send(response, 413, { error: "Request exceeds 1 MiB" });
return;
}
chunks.push(chunk);
}
const multipart = new Response(Buffer.concat(chunks), {
headers: { "Content-Type": contentType },
});
const form = await multipart.formData();
const file = form.get("attachment");
if (!(file instanceof File) || !file.name || file.size === 0) {
send(response, 400, { error: "Choose a nonempty file" });
return;
}
send(response, 200, { filename: file.name, bytes: file.size });
} catch {
send(response, 400, { error: "Could not read the upload" });
}
}).listen(3000, "127.0.0.1", () => {
console.log("Open http://127.0.0.1:3000");
});
စမ်းကြည့်ရင် ဘာဖြစ်မလဲ Upload အောင်မြင်ရင် {"filename":"notes.txt","bytes":12} လို JSON ကို မြင်ရပါမယ်။ Byte အရေအတွက်က ကိုယ်ရွေးတဲ့ဖိုင်ပေါ် မူတည်ပါတယ်။