Springboot + vue +element-ui 实现带参数上传文件功能

Springboot + vue +element-ui 实现带参数上传文件功能

element-ui 代码,上代码

这个功能是在报修系统中截取出来,上传标签中的 :data=“data” 对应data数据类型

<template>
<div>
  <div class="container">
    <div class="row clearfix">
      <div class="col-md-4 column">
      </div>
      <div class="col-md-4 column">
          <div class="row clearfix">
            <div class="col-md-12 column">
              <form role="form">
                <div class="form-group">
                  <label>故障描述</label><input type="text" v-model="repairInfo" class="form-control" id="exampleInputEmail1" />
                </div>
                <div class="form-group">
                  <label>故障地址</label><input type="text" v-model="repairPath" class="form-control" />
                </div>
                <div class="form-group">
                  <label>学号</label><input type="text" v-model="studentId" class="form-control" />
                </div>
                <el-select v-model="value" placeholder="选择故障类型">
                  <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
                <br>
                <br>
                <div class="form-group">
                  <label>上传故障图片</label>
                  <el-upload
                      class="upload-demo"
                      drag
                      action="http://localhost:8099/student/upload"
                      :data={repairInfo:this.repairInfo,repairPath:this.repairPath,studentId:this.studentId}
                      multiple>
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
                  </el-upload>
                  <p class="help-block">
                  </p>
                </div>
                <div class="checkbox">
                </div> <button type="submit" class="btn btn-default" v-on:click="submit">提交</button>
              </form>
            </div>
          </div>
      </div>
      <div class="col-md-4 column">
      </div>
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: "Student",
  data() {
    return {
      options: [{
        value: '选项1',
        label: '教学楼故障'
      }, {
        value: '选项2',
        label: '图书馆故障'
      }, {
        value: '选项3',
        label: '宿舍楼故障'
      }, {
        value: '选项4',
        label: '学校场所故障'
      }, {
        value: '选项5',
        label: '实验室故障'
      }],
      value: '',
      repairInfo : "",
      repairPath : "",
      repairType : "",
      studentId : "",
      data: {
        repairInfo : this.repairInfo,
        repairPath : this.repairPath,
        studentId : this.studentId
  }

    }
  },
  methods: {
    submit(){
      this.$ajax.post(base_url + "/student/repair",{
        repairInfo : this.repairInfo,
        repairPath : this.repairPath,
        repairType : this.value,
        studentId : this.studentId
      }).then(response => {
          if (response.data.code == 200){
            alert("提交成功")
            this.repairType="提交成功";
          }else {
            alert("提交失败")
            this.repairType="提交失败";
          }
      })
    }

  }
}
</script>

<style scoped>

</style>

pringboot接口

上代码

 @PostMapping("/upload")
    public Response teacherUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file,
                                  @RequestParam String repairInfo,
                                  @RequestParam String repairPath,
                                  @RequestParam String teacherId)
            throws IOException {
        Response<String> response = new Response<>();

        String fileName = file.getOriginalFilename();
        String newFileName = teacherId + "_" + repairPath + "_" + repairInfo +".jpg";
        // 得到文件保存的位置以及新文件名
        File dest = new File(System.getProperty("user.dir")+
                "/upload_photo/teacher/"+ newFileName);
        try {
            // 上传的文件被保存了
            file.transferTo(dest);
            // 打印日志
            System.out.println("上传成功,当前上传的文件保存在 {}"+ newFileName);
            // 自定义返回的统一的 JSON 格式的数据,可以直接返回这个字符串也是可以的。
            response.setCode(CodeEnum.SUCCESS.getCode());
        } catch (IOException e) {
            log.error("文件上传出错");
            response.setCode(CodeEnum.ERROR.getCode());
        }


        response.setCode(CodeEnum.SUCCESS.getCode());


        return response;
    }
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现头像上传,可以结合Spring Boot后端框架,Vue前端框架以及Element UI组件库进行实现。 首先,在Vue前端页面中,可以使用Element UI中的Upload组件实现文件上传功能。可以在页面中定义一个Upload组件,设置action属性为上传接口的URL,设置headers属性为请求头部信息,设置on-success属性为上传成功后的回调函数。具体代码如下: ``` <template> <div> <el-upload class=&quot;avatar-uploader&quot; action=&quot;/api/uploadAvatar&quot; :headers=&quot;{ Authorization: 'Bearer ' + token }&quot; :show-file-list=&quot;false&quot; :on-success=&quot;handleSuccess&quot;> <img v-if=&quot;imageUrl&quot; :src=&quot;imageUrl&quot; class=&quot;avatar&quot;> <i v-else class=&quot;el-icon-plus avatar-uploader-icon&quot;></i> </el-upload> </div> </template> <script> import { getToken } from '@/utils/auth' export default { data() { return { imageUrl: '', token: getToken() } }, methods: { handleSuccess(response) { this.imageUrl = response.data.url } } } </script> ``` 其中,token是用于认证的令牌,可以通过getToken函数获取。handleSuccess函数是上传成功后的回调函数,其中response.data.url表示上传成功后的图片URL。 然后,在Spring Boot后端接口中,可以使用Spring MVC的注解@RequestParam来接收上传的文件。具体代码如下: ``` @RestController @RequestMapping(&quot;/api&quot;) public class UploadController { @PostMapping(&quot;/uploadAvatar&quot;) public JsonResult uploadAvatar(@RequestParam(&quot;file&quot;) MultipartFile file) throws IOException { // 处理上传的文件 return JsonResult.ok(&quot;url&quot;, &quot;http://www.example.com/avatar.jpg&quot;); } } ``` 其中,@PostMapping注解表示接收POST请求,@RequestParam(&quot;file&quot;)注解表示接收名为file的文件参数。处理上传的文件后,可以返回一个JsonResult对象,其中包含上传成功后的图片URL。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值