SpringBoot+Vue前后端分离开发实现文件上传和在线预览

将用户上传的文件,下载本地并且将文件路径存入数据库

前端

vue-office目前只支持xlsx文件预览,不支持xls文件。所以在上传的时候需要设置accept属性限制用户可以选择的文件类型。

<input type="file" name="fileToUpload" id="fileToUpload" accept=".doc,.docx,.xlsx,.png,.jpg,.pdf,.txt">

 将用户上传的文件,下载本地

const fileInput = document.getElementById('fileToUpload');

const file = fileInput.files[0];

const formData = new FormData();

formData.append('file', file);

//文件下载本地

insertReport(formData).then(res => {

        //文件下载本地的相对路径插入数据库

        toUpdate(数据库表Id,文件下载本地的相对路径).then( r =>{
                  //插入数据库成功
        }).catch(error => {
          this.$message.error("失败!");
        });

})

 

 

//日报路径插入数据库
export function toUpdate(reportId,url){
  const data={
    reportId,
    url
  }
  return request({
    url: '/sysReport/toUpdate',
    method: 'post',
    data:data
  })
}


//日报下载本地
export function insertReport(fileData) {
  return request({
    contentType:false,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    url: '/sysReport/insertReport',
    method: 'post',
    data: fileData
  })
}

后端

@PostMapping("/insertReport")
public Result toInputImport(@RequestParam("file") MultipartFile getFile) throws IOException {
    return Result.success(this.sysReportService.toInputImport(getFile));
}

@PostMapping("/toUpdate")
public Result toUpdate(@RequestBody Map map){
    return Result.success(this.sysReportService.toUpdate(map));
}

 @Override
public String toInputImport(MultipartFile getFile) throws IOException {
    //定义全局唯一命名
    String unique = UUID.randomUUID().toString().replace("-","");
    //文件名
    String fileName = getFile.getOriginalFilename();
    //获取当前项目的根目录
    String rootPath = System.getProperty("user.dir");
    //定义相对路径
    String fileType = fileName.substring(fileName.lastIndexOf('.'));
    String partPath=unique+fileType;
    String relativePath="\\src\\main\\resources\\image\\toSaveReport\\"+ partPath;
    //拼接完整路径
    String fullPath=rootPath+relativePath;
    File file=new File(fullPath);
    if (!file.exists()){
        file.createNewFile();
    }else{
        file.delete();
        file.createNewFile();
    }
    getFile.transferTo(file);
    String url= "/image/toSaveReport/" +partPath;
    return url;
}

@Override
public String toUpdate(Map map) {
    Boolean aBoolean = this.sysReportMapper.toUpdate(Integer.parseInt(map.get("reportId").toString()), map.get("url").toString());
    if (aBoolean)
        return "文件路径插入数据库成功";
    else
        return "文件路径插入数据库失败";
}

需要注意的是yml和配置文件需要做相应配置

 

文件在线预览

前端

#docx文档预览组件

npm install @vue-office/docx vue-demi

#excel文档预览组件

npm install @vue-office/excel vue-demi

#pdf文档预览组件

npm install @vue-office/pdf vue-demi

如果是vue2.6版本或以下还需要额外安装 @vue/composition-api

npm install @vue/composition-api

//vue-office的使用 请百度查看vue-office简介

此外 

<iframe :src="getUrl"  frameborder="0" width="100%" height="500"></iframe>

也可以实现文件预览

例如:

<iframe :src="getUrl" v-if="getUrl.includes('.jpg') || getUrl.includes('.png')" frameborder="0" width="100%" height="500"></iframe> //预览照片
<iframe :src="getUrl" v-else-if="getUrl.includes('.txt')" frameborder="0" width="100%" height="500"></iframe> //预览txt文件

完结 

 

  • 40
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值