vue前端

参考:el-upload

使用方法

参数 默认值 说明
limit 1 一次只能最多上传几个文件
@fileList 自定义事件,获取子组件里上传的文件列表信息
  1. <FileUpload :limit="1" @fileList="getFileList" />
  2. <script>
  3. import FileUpload from '@/pages/models/filesupload/FileUpload.vue'
  4. export default {
  5. components: { FileUpload },
  6. data() {
  7. return {
  8. //文件列表信息
  9. fileList: [],}
  10. },
  11. methods: {
  12. //获取上传的文件信息
  13. getFileList(files) {
  14. console.log('收到的文件列表',files);
  15. //做相关逻辑
  16. },
  17. async tjSave(val){
  18. //添加formData数据
  19. console.log('this.form前', this.form);
  20. let formData = new FormData(); // new formData对象
  21. //添加修改的头像文件
  22. if (this.fileList.length > 0) {
  23. formData.append('file', this.fileList[0].raw)
  24. }
  25. //添加表单的数据
  26. formData.append('form', JSON.stringify(this.form))
  27. let res = await this.$api.base.pingyuFileNew(formData)
  28. }
  29. }
  30. }
  31. </script>

el-upload二次封装

  1. <template>
  2. <!-- 单个文件拖拽上传 -->
  3. <div class="tj-fileupload">
  4. <el-upload drag action="" :on-change="fileChange" :auto-upload="false" :limit="limit">
  5. <i class="el-icon-upload"></i>
  6. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  7. <div class="el-upload__tip" slot="tip">一次只能最多上传{{limit}}个文件,且单个文件不超过40MB</div>
  8. </el-upload>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. props: {
  14. // 上传文件数量限制
  15. limit: {
  16. type: Number,
  17. default: 1
  18. }
  19. },
  20. data() {
  21. return {
  22. }
  23. },
  24. methods: {
  25. fileChange(file, fileList) {
  26. console.log('当前上传的file', file);
  27. console.log('当前全部上传的fileList', fileList);
  28. this.$emit("fileList", fileList)
  29. },
  30. },
  31. }
  32. </script>
  33. <style lang="less">
  34. .tj-fileupload {
  35. .el-upload {
  36. width: 100%;
  37. }
  38. .el-upload-dragger {
  39. width: 100%;
  40. }
  41. }
  42. </style>

JAVA后端

新增

  1. /**
  2. * 新增
  3. *
  4. * @param form
  5. * @return
  6. */
  7. @PostMapping
  8. R<String> save(MultipartFile file, String form) throws IOException {
  9. if (StringUtils.isNotEmpty(form)) {
  10. //JSON字符串转JAVA对象
  11. BasePingyuFile basePingyuFile = JSON.parseObject(form, BasePingyuFile.class);
  12. if (basePingyuFileService.saveWithFile(file, basePingyuFile)) {
  13. return R.success("新增成功");
  14. }
  15. }
  16. return R.error("新增失败");
  17. }

新增程序

  1. /**
  2. * 文件新增带文件
  3. *
  4. * @param file 图片文件
  5. * @param basePingyuFile 实体类参数
  6. * @return Boolean
  7. */
  8. @Override
  9. public Boolean saveWithFile(MultipartFile file, BasePingyuFile basePingyuFile) throws IOException {
  10. try {
  11. //先保存到数据库,保存成功后,basePingyuFile会变成最新的数据,里面有id的信息
  12. save(basePingyuFile);
  13. log.info("保存后的数据内容:{}", basePingyuFile);
  14. //新增文件信息
  15. if (file != null && file.getSize() > 0) {
  16. //获取原始文件名的后缀
  17. String originalFilename = file.getOriginalFilename();
  18. log.info("文件名称:{}", originalFilename);
  19. String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
  20. //指定保存评语的路径
  21. String url = "TjFiles/pingyu/";
  22. //文件名称=店铺归属+ID+原文件后缀
  23. String filename = basePingyuFile.getShopbelong() + basePingyuFile.getId() + suffix;
  24. String filepath = TjFileUtils.handleUpFileRelativePath(file, url, filename);
  25. //把文件信息更新到表中
  26. basePingyuFile.setContenttype(suffix);
  27. basePingyuFile.setFilepath(filepath);
  28. }
  29. //再次更新,这里就不是新增了
  30. updateById(basePingyuFile);
  31. return true;
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. return false;
  35. }
  36. }

修改

  1. /**
  2. * 根据id修改信息
  3. *
  4. * @param file 图片文件
  5. * @param form 评语图片的form参数,是JSON字符串
  6. * @return
  7. */
  8. @PostMapping("/update")
  9. R<String> modify(MultipartFile file, String form) throws IOException {
  10. if (StringUtils.isNotEmpty(form)) {
  11. //JSON字符串转JAVA对象
  12. BasePingyuFile basePingyuFile = JSON.parseObject(form, BasePingyuFile.class);
  13. if (basePingyuFileService.updateWithFile(file,basePingyuFile)) {
  14. return R.success("修改成功");
  15. }
  16. }
  17. return R.error("修改失败");
  18. }

修改的程序

  1. /**
  2. * 文件更新带文件
  3. *
  4. * @param file 图片文件
  5. * @param basePingyuFile 实体类参数
  6. * @return Boolean
  7. */
  8. @Override
  9. public Boolean updateWithFile(MultipartFile file, BasePingyuFile basePingyuFile) {
  10. try {
  11. //更新文件信息
  12. if (file != null && file.getSize() > 0) {
  13. //获取原始文件名的后缀
  14. String originalFilename = file.getOriginalFilename();
  15. log.info("文件名称:{}", originalFilename);
  16. String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
  17. //指定保存评语的路径
  18. String url = "TjFiles/pingyu/";
  19. //文件名称=店铺归属+ID+原文件后缀
  20. String filename = basePingyuFile.getShopbelong() + basePingyuFile.getId() + suffix;
  21. String filepath = TjFileUtils.handleUpFileRelativePath(file, url, filename);
  22. //把文件信息更新到表中
  23. basePingyuFile.setContenttype(suffix);
  24. basePingyuFile.setFilepath(filepath);
  25. }
  26. //更新数据库
  27. updateById(basePingyuFile);
  28. return true;
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. return false;
  32. }
  33. }