vue 다운로드 파일 흐름 전체 전후 코드 구현

Vue를 사용할 때 백엔드에서 반환되는 파일 흐름을 처리하는 방법
우선 백엔드에서 흐르는 것을 되돌려줍니다. 여기서 흐르는 동작을 꺼냈습니다. 저는 많은 곳에서 사용해야 합니다.

/**
 *  
 *
 * @param docId
 */
 @GetMapping("/download/{docId}")
 public void download(@PathVariable("docId") String docId,
       HttpServletResponse response) {
  outWrite(response, docId);
 }
 
 /**
 *  
 * @param response
 * @param docId
 */
 private void outWrite(HttpServletResponse response, String docId) {
  ServletOutputStream out = null;
  try {
   out = response.getOutputStream();
   //  。
   response.setHeader("Pragma", "no-cache");
   response.setHeader("Cache-Control", "no-cache");
   response.setDateHeader("Expires", 0);
   byte[] bytes = docService.downloadFileSingle(docId);
   if (bytes != null) {
    MagicMatch match = Magic.getMagicMatch(bytes);
    String mimeType = match.getMimeType();
    response.setContentType(mimeType);
    out.write(bytes);
   }
   out.flush();
  } catch (Exception e) {
   UnitedLogger.error(e);
  } finally {
   IOUtils.closeQuietly(out);
  }
 }
앞에 구성 요소 js-file-download를 도입했습니다.

npm install js-file-download --save
그리고 Vue 파일에 추가합니다.

import fileDownload from "js-file-download";

 //  
 async handleCommand(item, data) {
  switch (item.key) {
  case "download":
   var res = await this.download(data);
   return fileDownload(res, data.name);
  ...
  default:
  }
  //  
  const folder = this.getLastFolderPath();
  this.listClick(folder.folderId, folder.name);
 },
 //  
 async download(row) {
  if (this.isFolder(row.type)) {
  return FolderAPI.download(row.id);
  } else {
  return DocAPI.download(row.id);
  }
 },
docAPI js responseType 설정 필요

/**
 *  
 * @param {*} id
 */
const download = (id) => {
 return request({
 url: _DataAPI.download + id,
 method: "GET",
 responseType: 'blob'
 });
};
이렇게 하면 성공적으로 다운로드할 수 있다.
vue에 관하여.js의 학습 강좌는 모두 주제를 클릭하십시오vue.js 구성 요소 학습 강좌Vue.js 전단 구성 요소 학습 강좌 공부를 합니다.
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기