乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當(dāng)前位置:首頁(yè) >  站長(zhǎng) >  編程技術(shù) >  正文

      vue實(shí)現(xiàn)下載文件流完整前后端代碼

       2020-11-19 14:22  來(lái)源: 腳本之家   我來(lái)投稿 撤稿糾錯(cuò)

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)下載文件流完整前后端代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

      使用Vue時(shí),我們前端如何處理后端返回的文件流

      首先后端返回流,這里我把流的動(dòng)作拿出來(lái)了,我很多地方要用

      /**
       * 下載單個(gè)文件
       *
       * @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);
        }
       }

      前端這里我引入了一個(gè)組件 js-file-download

      npm install js-file-download --save

      然后在Vue文件中添加進(jìn)來(lái)

      import fileDownload from "js-file-download";

       // 文檔操作列對(duì)應(yīng)事件
       async handleCommand(item, data) {
        switch (item.key) {
        case "download":
         var res = await this.download(data);
         return fileDownload(res, data.name);
        ...
        default:
        }
        // 刷新當(dāng)前層級(jí)的列表
        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 注意需要設(shè)置responseType

      /**
       * 下載單個(gè)文件
       * @param {*} id
       */
      const download = (id) => {
       return request({
       url: _DataAPI.download + id,
       method: "GET",
       responseType: 'blob'
       });
      };

      這樣即可成功下載。

      關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

      以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

      來(lái)源:腳本之家

      鏈接:https://www.jb51.net/article/199923.htm

      申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

      相關(guān)標(biāo)簽
      vue.js

      相關(guān)文章

      熱門排行

      信息推薦