_el-upload springboot">
当前位置:   article > 正文

el-upload与springboot使用_el-upload springboot

el-upload springboot

前端页面

  1. <template>
  2. <el-upload
  3. class="avatar-uploader"
  4. action="#"
  5. :show-file-list="false"
  6. :before-upload="beforeAvatarUpload"
  7. :http-request="uploadImg"
  8. >
  9. <i class="el-icon-plus avatar-uploader-icon imgbeforepad"></i>
  10. </el-upload>
  11. </template>
  12. <script>
  13. import request from '@/utils/request'
  14. export default {
  15. props: {
  16. uploadUrl: String, // 图片上传路径 例: '/ueditor/dynamic/imgUpload;
  17. },
  18. data () {
  19. return {}
  20. },
  21. methods: {
  22. uploadImg (f) {
  23. const file = f.file
  24. const form = new FormData()
  25. form.append('upfile', file)
  26. this.$http({
  27. url: request.adornUrl('map/upload/img'),
  28. method: 'post',
  29. data: form,
  30. processData: false,
  31. contentType: false,
  32. dataType: 'json',
  33. async: false,
  34. })
  35. .then(({ data }) => {
  36. debugger
  37. if (data && data.code === 0) {
  38. this.$emit('getImgUrl', data.data)
  39. } else {
  40. this.$message({
  41. message: '上传图片失败',
  42. type: 'warning',
  43. duration: 1500,
  44. })
  45. }
  46. this.loading = false
  47. })
  48. .catch(error => {
  49. this.loading = false
  50. console.log(error)
  51. })
  52. },
  53. // 上传图片前的过滤
  54. beforeAvatarUpload (file) {
  55. const isLt2M = (file.size / 1024 / 1024) < 1000
  56. // if (!isJPG) {
  57. // this.$message.error('上传图片只能是 JPG 格式!')
  58. // }
  59. if (!isLt2M) {
  60. this.$message.error('上传图片大小不能超过 2MB!')
  61. }
  62. return isLt2M
  63. },
  64. },
  65. }
  66. </script>
  67. <style>
  68. .avatar-uploader .el-upload {
  69. position: relative;
  70. overflow: hidden;
  71. cursor: pointer;
  72. border: 1px dashed #d9d9d9;
  73. border-radius: 6PX;
  74. }
  75. .avatar-uploader .el-upload:hover {
  76. border-color: #409EFF;
  77. }
  78. .avatar-uploader-icon {
  79. width: 50PX !important;
  80. height: 50PX !important;
  81. font-size: 28PX;
  82. line-height: 50PX !important;
  83. color: #8c939d;
  84. text-align: center;
  85. }
  86. .avatar {
  87. display: block;
  88. width: 50PX !important;
  89. height: 50PX !important;
  90. }
  91. .imgbeforepad{
  92. padding-top: 0px !important;
  93. }
  94. .avatar-uploader .el-icon-plus{
  95. margin: 15PX !important;
  96. }
  97. </style>

 

后端接收生成随机名称并存到相应位置,把地址传回前端。

  1. @PostMapping("/img")
  2. public Result img(@RequestParam("upfile") MultipartFile file) {
  3. try {
  4. if (file.isEmpty()) {
  5. return Result.failed("上传文件不能为空");
  6. }
  7. String originalFilename = file.getOriginalFilename();
  8. String filename = UUID.randomUUID().toString();
  9. String filetype = originalFilename.split("\\.")[1];
  10. if (filetype.indexOf("jpg") < 0 && filetype.indexOf("png") < 0) {
  11. return Result.failed("不支持类型");
  12. }
  13. String targetFileName = filename + "." + filetype;
  14. File dest = new File(new File(path).getAbsolutePath() + "/" + targetFileName);
  15. if (!dest.getParentFile().exists()) {
  16. dest.getParentFile().mkdirs();
  17. }
  18. file.transferTo(dest);
  19. return Result.ok(UPLOADER + targetFileName);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. return Result.failed(e.getMessage());
  23. }
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/384060
推荐阅读
相关标签