示例工程 27-file-upload
27-file-upload/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>文件上传 · accept / multiple / enctype</title>
<meta name="description" content="头像与文档上传、类型限制、多文件与服务端校验。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 32px 20px 40px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.page { width: min(100%, 760px); margin: 0 auto; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 28px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); line-height: 1.15; }
.lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 22px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
form { margin: 0; padding: 16px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
.field { margin-bottom: 16px; }
label { display: block; margin-bottom: 4px; font-size: 14px; color: #334155; }
input[type="file"] { width: 100%; max-width: 420px; padding: 6px; border: 1px dashed #94a3b8; border-radius: 6px; background: #f8fafc; font: inherit; font-size: 14px; }
.help { margin: 4px 0 0; color: #64748b; font-size: 13px; }
.error { margin: 4px 0 0; color: #b91c1c; font-size: 13px; }
.error::before { content: "错误:"; font-weight: 600; }
button { margin-top: 4px; padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 14px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
footer { margin-top: 30px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
body { padding: 20px 14px 32px; }
.site-header { flex-direction: column; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">FORM NOTES</span>
<nav aria-label="页内导航">
<a href="#avatar">头像</a>
<a href="#docs">文档</a>
<a href="#security">安全</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 27</p>
<h1>文件上传</h1>
<p class="lead">
文件上传不是「选个文件」就结束:要限制类型与大小、用对 <code>enctype</code>,并在服务器端重新校验。
</p>
<form action="preview.html" method="post" enctype="multipart/form-data">
<section id="avatar">
<h2>1. 头像</h2>
<div class="field">
<label for="avatar">头像图片</label>
<input
id="avatar"
name="avatar"
type="file"
accept="image/png,image/jpeg"
required
aria-describedby="avatar-help"
/>
<p class="help" id="avatar-help">PNG 或 JPEG,建议 400×400 以内,最大 2 MB。</p>
</div>
</section>
<section id="docs">
<h2>2. 文档(可多选)</h2>
<div class="field">
<label for="docs">上传材料</label>
<input
id="docs"
name="docs"
type="file"
accept=".pdf,.txt,application/pdf,text/plain"
multiple
aria-describedby="docs-help"
/>
<p class="help" id="docs-help">支持 PDF、TXT,单个文件不超过 10 MB,最多 5 个。</p>
</div>
</section>
<button type="submit">上传</button>
</form>
<section id="security">
<h2>3. 安全与规则</h2>
<ul>
<li><code>accept</code> 只是选择器提示,不是安全校验。</li>
<li>服务器要校验 MIME、扩展名、文件头与大小。</li>
<li>文件要重命名,存到 Web 根目录之外。</li>
<li>不要用用户文件名拼路径,防止目录穿越。</li>
<li>图片可以重新编码,去掉脚本与 EXIF。</li>
<li>上传进度与失败原因要可见。</li>
</ul>
<p class="note">表单要有 <code>enctype="multipart/form-data"</code> 且用 POST;GET 不能传文件。</p>
</section>
</main>
<footer>
<p>示例表单为虚构,仅用于教学。</p>
</footer>
</div>
</body>
</html>