• elementui实现图片和表单信息同时上传


    前端代码(vue框架):

    1. <template>
    2. <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    3. <el-form-item label="课程名称" prop="name">
    4. <el-input v-model="ruleForm.name">el-input>
    5. el-form-item>
    6. <el-form-item label="课程描述" prop="desc">
    7. <el-input type="textarea" v-model="ruleForm.desc">el-input>
    8. el-form-item>
    9. <el-form-item label="课程图片">
    10. <el-upload
    11. class="avatar-uploader"
    12. action="#"
    13. ref="upload"
    14. :http-request="httprequest"
    15. :show-file-list="false"
    16. :auto-upload="false"
    17. :on-change="hanchange"
    18. >
    19. <img v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar">
    20. <i v-else class="el-icon-plus avatar-uploader-icon">i>
    21. el-upload>
    22. el-form-item>
    23. <el-form-item>
    24. <el-button type="primary" @click="submitForm('ruleForm')">立即创建el-button>
    25. <el-button @click="resetForm('ruleForm')">重置el-button>
    26. el-form-item>
    27. el-form>
    28. template>
    29. <script>
    30. import axios from "axios";
    31. export default {
    32. data() {
    33. return {
    34. ruleForm: {
    35. name: '',
    36. desc: '',
    37. imageUrl:"",
    38. },
    39. rules: {
    40. name: [
    41. { required: true, message: '请输入课程名称', trigger: 'blur' },
    42. { max: 20, message: '最多20个字符', trigger: 'blur' }
    43. ],
    44. desc: [
    45. { required: true, message: '请填写课程描述', trigger: 'blur' },
    46. {max:20,message:'描述最多20个字符',trigger: "blur"}
    47. ]
    48. }
    49. };
    50. },
    51. methods: {
    52. httprequest(param){
    53. //拷贝图片到表单数据之中
    54. this.ruleForm.file=param.file
    55. },
    56. submitForm(formName) {
    57. this.$refs[formName].validate((valid) => {
    58. if (valid) {
    59. this.$refs.upload.submit()
    60. let formdata=new FormData()
    61. formdata.append("name",this.ruleForm.name)
    62. formdata.append("desc",this.ruleForm.desc)
    63. formdata.append("file",this.ruleForm.file)
    64. let config={
    65. header:{
    66. "Content-Type": "multipart/form-data"
    67. }
    68. }
    69. console.log(formdata)
    70. axios.post("http://localhost:8181/image",formdata,config)
    71. console.log("submit")
    72. } else {
    73. console.log('error submit!!');
    74. return false;
    75. }
    76. });
    77. },
    78. resetForm(formName) {
    79. this.$refs[formName].resetFields();
    80. this.ruleForm.imageUrl=''
    81. },
    82. hanchange(file){
    83. //显示图片
    84. this.ruleForm.imageUrl= URL.createObjectURL(file.raw);
    85. }
    86. }
    87. }
    88. script>
    89. <style>
    90. .avatar-uploader .el-upload {
    91. border: 1px dashed #d9d9d9;
    92. border-radius: 6px;
    93. cursor: pointer;
    94. position: relative;
    95. overflow: hidden;
    96. }
    97. .avatar-uploader .el-upload:hover {
    98. border-color: #409EFF;
    99. }
    100. .avatar-uploader-icon {
    101. font-size: 28px;
    102. color: #8c939d;
    103. width: 178px;
    104. height: 178px;
    105. line-height: 178px;
    106. text-align: center;
    107. }
    108. .avatar {
    109. width: 178px;
    110. height: 178px;
    111. display: block;
    112. }
    113. style>

    后端接口:(跨域请求这里小编使用的添加@CrossOrigin()注解)

    1. @Controller
    2. @ResponseBody
    3. //跨越请求
    4. @CrossOrigin("http://localhost:7070")
    5. public class webcontrller {
    6. //普通查询
    7. @Autowired
    8. private userservice userservice;
    9. @GetMapping("/users")
    10. public List GetAlluser(){
    11. List users = userservice.selectList(null);
    12. return users;
    13. }
    14. //简单的分页效果的查询
    15. @GetMapping("/pages")
    16. public PageInfopages(@RequestParam(defaultValue = "5") Integer pagesize,
    17. @RequestParam(defaultValue ="1") Integer pageNum ){
    18. PageHelper.startPage(pageNum,pagesize);
    19. PageInfopageInfo=new PageInfo<>(userservice.selectList(null));
    20. return pageInfo;
    21. }
    22. //头像上传回显
    23. @PostMapping("/image")
    24. public String url(MultipartFile file,String name,String desc) throws IOException {
    25. //获取图片的原始名字
    26. String originalFilename = file.getOriginalFilename();
    27. //获取文件的后缀
    28. String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
    29. //设置uuid
    30. String uuid= UUID.randomUUID().toString();
    31. //设置新的图片名字
    32. String filename=uuid+substring;
    33. System.out.println(filename);
    34. //获取文件的路径
    35. String path= "D:/网站素材图片/";
    36. file.transferTo(new File(path,filename));
    37. System.out.println(name+desc);
    38. return path+filename;
    39. }
    40. }

    执行结果: 

     

  • 相关阅读:
    STM32—按键控制LED(定时器)
    c++类相互引用的问题,如何避免头文件相互包含
    【Pytorch】torch.Tensor.view()
    像设计硬件线路那样设计软件
    首次做CMMI,如何选择适合的评估级别
    【技术追踪】SAM(Segment Anything Model)代码解析与结构绘制之Image Encoder
    Maven仓库地址(寻找Maven依赖)
    MySQL流程控制函数
    第59篇 QML 之 JS类型转换为 Number 类型
    一文搞懂什么是 GNU/Linux 操作系统
  • 原文地址:https://blog.csdn.net/qq_53909398/article/details/127777716