示例工程 27-file-upload

27-file-upload/demo-01-basic/index.html

html · 2.1 KB

运行:单文件上传演示

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>单文件上传演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
      .box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      h1 { margin: 0 0 16px; font-size: 28px; }
      h2 { margin: 0 0 8px; font-size: 17px; }
      p { margin: 0 0 10px; color: #334155; }
      label { display: block; margin: 6px 0 4px; font-size: 14px; }
      input[type="file"] { width: 100%; max-width: 360px; padding: 6px; border: 1px dashed #94a3b8; border-radius: 6px; background: #f8fafc; font: inherit; }
      .help { color: #64748b; font-size: 13px; }
      button { margin-top: 8px; padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
      pre { margin: 8px 0 0; padding: 12px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
    </style>
  </head>
  <body>
    <h1>单文件上传</h1>

    <div class="box">
      <h2>头像</h2>
      <form action="preview.html" method="post" enctype="multipart/form-data">
        <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,最大 2 MB。</p>
        <button type="submit">上传</button>
      </form>
    </div>

    <div class="box">
      <h2>关键属性</h2>
      <pre>&lt;form method="post" enctype="multipart/form-data"&gt;
  &lt;input type="file" name="avatar"
         accept="image/png,image/jpeg" required&gt;
&lt;/form&gt;</pre>
      <p><code>accept</code> 只过滤选择器,不能阻止用户改扩展名。</p>
    </div>
  </body>
</html>