Springboot + Vue 上传Word、PDF文档并保留内部格式

因为业务需求,上传Word文件需要编辑,但如果使用Blob方式,在数据库里存文件,就会造成格式消失。所以修改思路:上传文件到服务器本地,保证数据存储的完整性。

前端

              <el-upload class="upload-demo" :action="item.fileUploadUrl" :show-file-list="false" :limit="1" :multiple="false" :auto-upload="true" :accept="item.accept" :before-upload="beforeUpload"
                :headers="item.uploadHeaders" :data="item.uploadData">上传
              </el-upload></el-button>
              
     fileActionList: [
        {
          width: 60,
          name: "合同文件",
          fileUploadUrl: "*************/importContract",
          uploadData: {},
          uploadHeaders: {},
          accept: ".doc,.docx,.pdf",
        },
      ],



    beforeUpload(file, fileList) {
      let promise = new Promise((resolve) => {
        this.$nextTick(function () {
          resolve(true);
        });
      });
      this.$message.success("上传成功");

      return promise;
    },
        // 文件上传操作
    handleImport(row) {
      this.fileActionList[0].uploadData = {
        id: row.id,
      };
      this.fileActionList[0].uploadHeaders = {
        Authorization: Cookie.get("*******"),
      };
    },

就是常规的传文件

后端

    @ApiOperation("导入合同文件")
    @PostMapping("importContract")
    public Result importData(@RequestParam(value="file") MultipartFile file, @RequestParam(value="id") Long id) throws Exception {
        return projectService.importData(file, id);
    }
        @Override
    public Result importData(MultipartFile file, Long id) throws Exception {
        //获取文件名
        InputStream path = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            path = file.getInputStream();
            fis = (FileInputStream) path;
            File dir = new File("file/project");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = "file/project/" + id + "_" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
            fos = new FileOutputStream(filePath);
            int readlen = 0;
            //字节数组,一次读取8个字节
            byte[] buf = new byte[8];
            while ((readlen = fis.read(buf)) != -1) {
                fos.write(buf, 0, readlen);
            }
            Project project = projectRepo.getById(id);
            project.setContactFile(filePath);
            projectRepo.save(project);
        } catch (IOException e) {
            log.error("" + e);
        } finally {
            path.close();
            fis.close();
            fos.close();
        }
        return Result.success("上传成功");
    }

这此采用的思路就是 把文件读取后,写入相对路径,考虑到文件业务误传,服务器源文件不删除,通过时间戳保证唯一性。存到服务器本地。

可以看到,最后格式都保留了下来

![在这里插入图片描述](https://img-blog.csdnimg.cn/6c7c61ef8085404ea23c4be0ca6c6f19.png在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Vue上传Word文件可以通过以下步骤实现: 1. 在Vue组件中创建一个文件选择器input,用于选择Word文件: ``` <input type="file" ref="fileInput" @change="handleFileUpload" accept=".doc, .docx" /> ``` 2. 创建一个方法来处理文件上传事件: ``` methods: { handleFileUpload() { const file = this.$refs.fileInput.files[0]; // 在这里可以对文件进行处理,比如验证文件类型、大小等 } } ``` 3. 在handleFileUpload方法中,我们可以使用FormData对象来构建一个表单,并将文件添加到表单中: ``` handleFileUpload() { const file = this.$refs.fileInput.files[0]; const formData = new FormData(); formData.append('file', file); // 在这里可以执行上传操作,比如通过axios发送请求将文件上传到服务器 } ``` 4. 在上传操作中,可以使用axios库来发送POST请求并将FormData对象作为请求体发送到服务器: ``` import axios from 'axios'; handleFileUpload() { const file = this.$refs.fileInput.files[0]; const formData = new FormData(); formData.append('file', file); axios.post('/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' } }).then(response => { // 上传成功后的处理逻辑 }).catch(error => { // 上传失败后的处理逻辑 }); } ``` 5. 在服务器端,你需要相应的后端处理来接收并保存上传Word文件。 以上就是使用Vue上传Word文件的简单步骤,你可以根据项目需求进行相应的扩展和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值