我的前端坑
  • 关于本书
  • CSS JS 常用
    • 常用 CSS 样式
    • 坑爹的 CSS
    • sass 入门
    • canvas 总结
    • 常用 JS 函数
    • 表单和 FormData 对象
    • 水平滑动 tab 居中
    • js 中的 this
    • sse 和 fetch
    • js 原型链与 class 类
  • TypeScript
    • TS 概念
    • interface 与 type
    • interface 接口
    • Ts 配合 ESLint 和 prettier
  • 小程序
    • 常用小程序代码
  • VUE
    • VUE2 小技巧
    • VUE-CLI 中的 NODE_ENV
  • VUE3
    • VUE3 自行构建 sfc 遇到的坑
    • VUE3 v-model 的实现
    • VUE3 使用总结
    • VUE3 ref
  • vite
    • vite
  • http 请求
    • 前端实现下载
    • cors 跨域请求
    • windows hosts 文件
    • err_blocked_by_client 错误
  • 前端工具
    • npm 和 Node
      • 常见问题
      • npmTips
      • package.json 与 package-lock.json
      • npx
      • exports 与 module.exports
      • ESLint
    • VIM
      • vim 常用
    • Git
      • Git 常用命令
      • Git 小 tips
    • express
  • 后端工具
    • mysql 常见问题
    • docker 常见问题
    • docker
  • java
    • java 常用
    • lambda 表达式
    • java 字符串
    • java 泛型
    • java 反射
    • intellij IDEA
    • 多态
    • java 包管理
    • sql 查询语言
    • java 反射
    • java 异常
    • java 集合
    • spring
  • 命令行
    • 命令行 常用
  • 专利撰写 ppt
  • 后台简述
Powered by GitBook
On this page
  • form 表单中的 method 和 enctype 属性
  • 文件上传

Was this helpful?

  1. CSS JS 常用

表单和 FormData 对象

form 表单中的 method 和 enctype 属性

  1. 当 method="get" 时,enctype 属性无效,数据将以 URL 的查询字符串发出

    GET /handling-page?user_name=张三&user_passwd=123&submit_button=提交
    Host: example.com
  2. 当 method="post" 时,enctype 属性默认为 'application/ x-www-form-urlencoded',所有键值对会连接成一行,作为 HTTP 请求 的数据体发送到服务器

    POST /handling-page HTTP/1.1
    Host: example.com
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 74
    
    user_name=张三&user_passwd=123&submit_button=提交
  3. 当 method="post",enctype = 'text/plain' 时,数据将以纯文本格式发送

  4. 当 method="post",enctype = 'multipart/form-data',数据将以混合的格式发送,这种格式也是文件上传的格式

    Content-Type: multipart/form-data; boundary=---------------------------314911788813839
    
    -----------------------------314911788813839
    Content-Disposition: form-data; name="foo"
    
    bar
    -----------------------------314911788813839
    Content-Disposition: form-data; name="baz"
    
    The first line.
    The second line.
    
    -----------------------------314911788813839--

文件上传

本质就是:通过文件输入框选择本地文件,提交表单的时候,浏览器就会把这个文件发送到服务器。 需要将表单 form 元素的 method 属性设为 POST,enctype 属性设为 multipart/form-data

例子(使用自定义脚本上传):

<div>
  <label for="up-img" class="label-style">上传图片</label>
  <input id="up-img" type="file" accept="image/*" style="opacity:0;width:0;henght:0;" />
</div>
<script>
  const input = document.getElementById('up-img')
  input.addEventListener('change', function (e) {
    console.log(e.target.files) // 等价于 console.log(input.value),返回的是一个 FileList 对象
    const upInfo = e.target.files[0]
    const upForm = new FormData()
    upForm.append('file', upInfo)
    // 进行上传操作
    // request.post('xxx/xx/xx',upForm)
  })
</script>

补充: 文件选择器<input type="file">,出于安全考虑,浏览器不允许脚本自行设置这个控件的 value 属性,即文件必须是用 户手动选取的,不能是脚本指定的。一旦用户选好了文件,脚本就可以读取这个文件,返回一个 FileList 对象,每个成员都是一个 File 实例对象。File 实例对象是一个特殊的 Blob 实例,增加了name和lastModifiedDate属性。

Previous常用 JS 函数Next水平滑动 tab 居中

Last updated 4 years ago

Was this helpful?