js 下载文件流

<template>
  <div class="expand-notes" id="attachment">
    <div>
      <h4 class="h-list step7">attachment Download | 附件下载 </h4>
    </div>
    <!--附件下载-->
    <div class="language">
      <el-table :data="filelist" border style="width: 100%">
        <el-table-column prop="fileName" align='center' label="文件名称">
        </el-table-column>
        <el-table-column fixed="right" label="操作" align='center'>
          <template slot-scope="scope" >
            <el-button size="small"  type="primary" plain @click="downloadfiles(scope.row)" :loading="scope.row.isloading">{{scope.row.isloading ? "下载中..." : "下载" }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { downloadfilelist} from '@/api';
export default {
  name: 'attachment',
  props:{
    filelist:{
      type:Array,
      default:()=>{
        return []
      }
    }
  },
  data () {
    return {}
  },
  methods: {
    downloadfiles(rows){
      rows.isloading = true;
      downloadfilelist(rows.id).then(res => {
          const content = res.data;
          const blob = new Blob([content])
          const filename = rows.fileName;
          if('download' in document.createElement('a')) {
            const elink = document.createElement('a')
            elink.download = filename
            elink.style.display = "none"
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click();
            URL.revokeObjectURL(elink.href)
            document.body.removeChild(elink)
          } else {
            navigator.msSaveBlob(blob, filename)
          }
        }).catch(err=>{
          this.$message.error(err ||'下载失败,请稍后再试!');
        })
        .finally(()=>{
           rows.isloading = false;
        })
    }
  }
}
</script>

  

上一篇:robots


下一篇:blob 请求后台,后台返回文件流,利用ajax或者axios请求返回下载文件