springboot通过表单同时提交字段和上传文件

通过form表单同时提交字段和上传文件

单独提交字段或者上传文件都比较简单,这里说一下如何同时提交被后端接收。

前端

  1. 字段设置:
    通过input的name属性,和后端的类的field名字对应,使后端拿到该属性。
  2. 文件设置:
    通过form表单集成的 for=“file” 或者 for=“files” 属性,上传单个或多个文件,这里同样需要制定name属性,和后端接收的名字一致。
  3. 注意的大坑:form表单里button调用的方法名,不能和button的id一致!
  4. html代码如下:
<div class="container">
    <form class="form-signin" id="forms" method="post" enctype="multipart/form-data">
        <h2 class="form-signin-heading">表头</h2>
        <div class="input-group">
        <input th:id="field1" name="field1" placeholder="字段1"/>
        <input th:
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在端,可以使用 Element UI 的 Upload 组件来上传文件,同时将表单数据也一并提交到后端。具体代码如下: ```html <template> <el-form :model="form" label-width="120px"> <el-form-item label="姓名"> <el-input v-model="form.name"></el-input> </el-form-item> <el-form-item label="头像"> <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> </el-form-item> </el-form> </template> <script> export default { data() { return { form: { name: '', avatar: '' }, imageUrl: '' } }, methods: { handleAvatarSuccess(res, file) { this.form.avatar = res.url this.imageUrl = URL.createObjectURL(file.raw) }, beforeAvatarUpload(file) { // 限制上传图片的大小和格式 const isJPG = file.type === 'image/jpeg' const isPNG = file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG && !isPNG) { this.$message.error('上传头像图片只能是 JPG/PNG 格式!') return false } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!') return false } return true }, submitForm() { this.$refs.form.validate(valid => { if (valid) { const formData = new FormData() formData.append('name', this.form.name) formData.append('avatar', this.form.avatar) // 将表单数据和文件一起提交到后端 this.$http.post('/api/user', formData).then(res => { this.$message.success('提交成功') }).catch(error => { this.$message.error('提交失败') }) } else { this.$message.error('表单验证失败') return false } }) } } } </script> ``` 在后端,使用 Spring Boot 的 MultipartFile 类型来接收上传的文件,并在 Controller 处理表单数据和文件上传的逻辑。具体代码如下: ```java @RestController @RequestMapping("/api") public class UserController { @PostMapping("/user") public ResponseEntity<?> createUser(@RequestParam("name") String name, @RequestParam("avatar") MultipartFile avatar) { // 处理表单数据和文件上传的逻辑 // ... return ResponseEntity.ok().body("创建用户成功"); } } ``` 需要注意的是,由于使用了 FormData 对象来提交表单数据和文件,因此需要在前端使用 axios 或者其他 HTTP 请求库来发送 POST 请求。在上面的代码,我使用了 Vue.js 的官方插件 vue-resource 来发送 POST 请求。如果您使用的是 axios,则可以将代码修改为: ```javascript import axios from 'axios' submitForm() { this.$refs.form.validate(valid => { if (valid) { const formData = new FormData() formData.append('name', this.form.name) formData.append('avatar', this.form.avatar) // 将表单数据和文件一起提交到后端 axios.post('/api/user', formData).then(res => { this.$message.success('提交成功') }).catch(error => { this.$message.error('提交失败') }) } else { this.$message.error('表单验证失败') return false } }) } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值