Spring Boot + Vue + Element-UI 实现文件下载

本文介绍了如何在SpringBoot项目中创建一个后端接口,用于处理文件下载请求,并配合前端Vue.js中的El-Table实现文件下载功能。通过@Autowired注解注入服务,处理文件信息并设置合适的HTTP头以实现文件下载。
摘要由CSDN通过智能技术生成

实现效果:点击链接,下载对应文件

后端接口:

import com.example.admin.entity.FileEntity;
import com.example.admin.service.FileService;
import com.example.admin.utils.Result;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    // 根据文件id查询文件信息
    @GetMapping("/getFile")
    public Result<FileEntity> getFile(String fid) {
        return fileService.getFileById(fid);
    }

    // 文件下载
    @GetMapping("/download")
    public void fileDownload(String fid, HttpServletResponse response) throws IOException {
        FileEntity fileEntity = fileService.getFileById(fid).getData();
        String fname = fileEntity.getFname();
        String path = fileEntity.getPath();
        File file = new File(path);
        FileInputStream is = new FileInputStream(file);
        response.setContentType("application/x-download;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fname, "UTF-8"));
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(is, os);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }

}

前端代码:

<el-table-column label="下载文件" prop="licence1">
  <template slot-scope="scope">
    <!-- 使用 scoped slot 自定义单元格内容 -->
    <el-link @click="download(scope.row.licence1)">
      {{ scope.row.licence1 }}
    </el-link>
  </template>
</el-table-column>
/* 下载文件 */
async download(fid) {
  try {
    const response = await this.$axios.get("/api/file/download", {
      params: {
        fid: fid,
      },
      responseType: "arraybuffer",
    });
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement("a");
    link.href = url;
    // 获取文件信息
    const fileInfoResponse = await this.$axios.get("/api/file/getFile", {
      params: {
        fid: fid,
      },
    });
    const fname = fileInfoResponse.data.data.fname;
    link.setAttribute("download", `${fname}`);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
  } catch (error) {
    console.error("Download error:", error);
  }
},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值