element的上传如何获取路径_vue+springboot+element+vue-resource实现文件上传教程

vue页面设置

class="upload-demo"

action=""

:before-upload="beforeUpload" //上传前操作

:before-remove="beforeRemove" //移除钱操作

:multiple="false" //禁止多选

:http-request="myUpload" //文件上传,重写文件上传方法,action的路径不会起作用

accept=".jar" //限制文件选择类型

:drag="false"

:data="param" //参数

:file-list="fileList">//文件显示列表

点击上传

只能上传jar文件,且不超过500kb

/*文件上传前,判断文件名是否存在,等其他处理*/

beforeUpload(file){

console.log("文件名",file.name,this.fileList)

for (let i = 0; i

if (this.fileList[i].name==file.name) {

this.$message.info("文件已存在");

return false;

}

}

this.file=file;

return true;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值