美文网首页
vue 如何下载文件

vue 如何下载文件

作者: Cute_小肥鸡 | 来源:发表于2022-01-26 10:46 被阅读0次

项目要求:需要下载线上的文件,如果用<a href=".../1.jpg" download></a>,直接打开图片,不会将文件下载到本地,只能用以下方法解决

  • download.js
/* 下载 */
export function downloadFiles(filePath, fileName) {
  fetch(filePath).then(res => res.blob()).then(blob => {
    const a = document.createElement("a");
    a.href = URL.createObjectURL(blob);
    a.download = fileName;
    document.body.appendChild(a); //追加a标签
    a.click(); //a标签点击
    document.body.removeChild(a); //移除节点
  })
}
  • 在具体页面调用 下载JS
<!-- 预览文件(包括图片、文档等) -->
<template>
  <div class="attPreview flexColumn">
    <!-- 头部 -->
    <div class="previewHeader">
      <div class="flexRow">
        <div class="editableBlock editName" :title="fileInfo.FIName">
          <span class="content">
            <span class="fileName ellipsis" style="min-width:8px;">{{ fileInfo.FIName }}</span>
          </span>
        </div>
        <div class="flex"></div>
        <a class="comm-preA InlineBlock Hand preDoc_download" title="下载" @click="clickDownload(fileInfo)">
          <vab-icon icon="download-fill" />
        </a>
        <div class="attClose Hand" title="关闭" @click="closeAttPreview">
          <vab-icon icon="close-fill" />
        </div>
      </div>
    </div>
    <!-- 内容 -->
    <div class="previewPanel">
      <div class="previewContainer">
        <!-- 图片 -->
        <div class="dragAbleContainer fileViewer imageViewer"
          v-if="currentExt=='png' || currentExt=='gif' || currentExt=='bmp' || currentExt=='jpg' || currentExt=='jpeg'">
          <img class="dragAbleImg noSelect grab" id="gvh_imgsrc" :src="fileInfo.FFilePath" />
        </div>
        <!-- 文档 -->
        <div class="ifContainer"
          v-else-if="currentExt=='pptx' || currentExt=='ppt' || currentExt=='pot' || currentExt=='potx' || currentExt=='pps' || currentExt=='ppsx' || currentExt=='dps' || currentExt=='dpt' || currentExt=='pptm' || currentExt=='potm' || currentExt=='ppsm' || currentExt=='xls' || currentExt=='xlt' || currentExt=='et' || currentExt=='ett' || currentExt=='xlsx' || currentExt=='xltx' || currentExt=='csv' || currentExt=='xlsb' || currentExt=='xlsm' || currentExt=='xltm' || currentExt=='doc' || currentExt=='dot' || currentExt=='wps' || currentExt=='wpt' || currentExt=='docx' || currentExt=='dotx' || currentExt=='docm' || currentExt=='dotm' || currentExt=='lrc' || currentExt=='c' || currentExt=='cpp' || currentExt=='cmd' || currentExt=='rtf' || currentExt=='txt' || currentExt=='log' || currentExt=='xml' || currentExt=='htm' || currentExt=='html' || currentExt=='pdf'">
          <iframe id="preDocContent" width="100%" height="100%"
            :src="fileInfo.FFilePath+'#scrollbars=0&toolbar=0&statusbar=0'" type="application/pdf"
            style="border:none;"></iframe>
        </div>
        <!-- 视频 -->
        <!-- 其它 -->
        <div class="canNotView"
          v-else-if="currentExt=='wmv' || currentExt=='asf' || currentExt=='asx' || currentExt=='rm' || currentExt=='rmvb' || currentExt=='mp3' || currentExt=='mp4' || currentExt=='mpg' || currentExt=='mpeg' || currentExt=='3gp' || currentExt=='mov' || currentExt=='m4v' || currentExt=='avi' || currentExt=='dat' || currentExt=='mkv' || currentExt=='flv' || currentExt=='vob'">
          <attIcon :ext="currentExt"></attIcon>
          <p class="fileName">{{ fileInfo.FIName }}</p>
          <a class="btn btn-primary downloadBtn" @click="clickDownload(fileInfo)">下载</a>
        </div>
      </div>
    </div>
    <!-- 底部 -->
    <div class="thumbnailGuide"
      v-if="currentExt=='png' || currentExt=='gif' || currentExt=='bmp' || currentExt=='jpg' || currentExt=='jpeg'">
      <div class="statusBar fle">
        <div class="fold">
          <div class="InlineBlock">
            <div class="iOperate Hand">
              <vab-icon icon="zoom-in-line" title="放大" @click="enlargeImg" />
              <vab-icon icon="zoom-out-line" title="缩小" @click="narrowImg" />
              <el-icon class="io_refresh" title="旋转" @click="rotateImg">
                <refresh-left />
              </el-icon>
              <a class="originImage Hand noSelect" :href="fileInfo.FFilePath" target="_blank" title="查看图片">原图</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { defineComponent, defineAsyncComponent, onMounted, reactive, toRefs } from 'vue'
  import { getFilesInfo } from '../../../api/file.js'
  import { downloadFiles } from "@/utils/download.js";

  export default {
    name: 'PreviewFiles',
    components: {
      attIcon: defineAsyncComponent(() => import('../../components/att/index.vue'))
    },
    props: {
      id: {
        type: String,
        default: ''
      },
      ext: {
        type: String,
        default: ''
      }
    },
    emits: ['close'],
    setup(props, { emit }) {
      //////////////////////////////////////////////////////////////////////////////////////////1、定义变量
      const state = reactive({
        fileInfo: [], //文件详情
        currentID: props.id,
        currentExt: props.ext
      })


      //////////////////////////////////////////////////////////////////////////////////////////2、关闭
      const closeAttPreview = () => {
        emit("close", 0)
      }


      //////////////////////////////////////////////////////////////////////////////////////////3、获取文件详情
      const fetchData = async () => {
        var resultData = await getFilesInfo({
          'token': localStorage.getItem('access_token'),
          "l_i": state.currentID,
          "f_t": state.currentExt
        })
        if (resultData["code"] == 200) {
          state.fileInfo = resultData["data"][0]
        } else {
          state.fileInfo = []
        }
      }
      onMounted(() => {
        fetchData()
      })


      //////////////////////////////////////////////////////////////////////////////////////////4、下载
      const clickDownload = (fileInfo) => {
        downloadFiles(fileInfo.FFilePath, fileInfo.FIName);
      }


      //////////////////////////////////////////////////////////////////////////////////////////5、放大图片
      const enlargeImg = () => {
        var oImg = document.getElementById("gvh_imgsrc");
        oImg.style.width = (oImg.width * 1.2) + "px";
        oImg.style.height = (oImg.height * 1.2) + "px";
      }


      //////////////////////////////////////////////////////////////////////////////////////////6、缩小图片
      const narrowImg = () => {
        var oImg = document.getElementById("gvh_imgsrc");
        var widthImg = oImg.width / 1.2;
        var heightImg = oImg.height / 1.2;

        if (widthImg > 250) {
          oImg.style.width = widthImg + "px";
          oImg.style.height = heightImg + "px";
        }
      }


      //////////////////////////////////////////////////////////////////////////////////////////7、旋转图片
      var current = 0;
      const rotateImg = () => {
        current = (current + 90) % 360;
        var oImg = document.getElementById("gvh_imgsrc");
        oImg.style.transform = 'rotate(' + current + 'deg)';
      }




      return {
        ...toRefs(state),

        closeAttPreview,
        clickDownload,
        enlargeImg,
        narrowImg,
        rotateImg
      }
    }
  }
</script>

<style>
</style>
  • 效果图
效果图

相关文章

  • vue学习第一天笔记

    VUE 下载和安装vue官网npm下载vue模块:npm install vue下载完后使用里面的vue.js文件...

  • vue 如何下载文件

    项目要求:需要下载线上的文件,如果用 ,直接打开图片,不会将文件下载到本地,只能用以下方法解决 download...

  • wangEditor4

    1.下载 vue文件

  • vue循环下载多个文件

    vue循环下载多个文件

  • vue项目使用 富文本插件

    下载 npm i vue-ueditor-wrap -S下载 UEditor 文件放到 public 文件下面 ...

  • echarts 立体柱形图

    如图: 下载echarts:版本如下: vue文件:

  • vue文件下载

    需求:vue直接下载后台返回Excel文件 请求后台文件的时候需加入responseType:'applicati...

  • vue第一天总结

    1.下载并且安装vue.js,输入指令 upm install vue,下载成功之后出现文件即可。 vue相对于A...

  • vue 日常使用

    一. vue 使用 pdf 插件 使用Vue Cli生成项目 下载pdf.js 解压下载后的文件,将文件夹里面的b...

  • 2018-09-11

    vue第一天总结 1.下载并且安装vue.js,输入指令 upm install vue,下载成功之后出现文件即...

网友评论

      本文标题:vue 如何下载文件

      本文链接:https://www.haomeiwen.com/subject/hbcnhrtx.html