vue element el-upload附件上传、在线预览、下载当前预览文件

  • 上传

  • 在线预览(iframe):
  • payload:
  • response:

  • 全部代码:
javascript 复制代码
<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="date" label="日期"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="province" label="省份"></el-table-column>
      <el-table-column prop="city" label="市区"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="zip" label="邮编"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small">编辑</el-button>
          <el-button @click="handleClick(scope.row)" type="text" size="small">上传文件</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-upload
      class="upload-demo"
      action="default"
      ref="uploadFile"
      style="display: none"
      :http-request="uploadFilePdf"
      :on-success="handleSuccess"
      :limit="1"
      :show-file-list="false"
    >
      <el-button type="default" ref="upload">上传文件</el-button>
    </el-upload>
    <el-dialog
      v-if="viewDlg"
      :title="viewDlgTitle"
      class="prj-view-dlg"
      :visible.sync="viewDlg"
      :fullscreen="true"
      append-to-body
      v-loading="diaLoading"
    >
      <div class="iframe" style="height: 100%">
        <iframe
          :src="pageUrl"
          frameborder="0"
          height="100%"
          width="100%"
          scrolling="no"
          name="demo"
        ></iframe>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="downloadFile">下 载</el-button>
        <el-button @click="viewDlg = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: "test",
  methods: {
    // 上传
    uploadFilePdf(params) {
      // this.tableLoading = true;
      const file = params.file;
      let lastName = file.name
        .substring(file.name.lastIndexOf("."), file.name.length)
        .toLowerCase();
      if (lastName == ".pdf") {
        var formData = new FormData();
        formData.append("file", file); // 'file'名称要与后台约定好 一致
        axios.post(`/xxxxxx/${this.currentRowData.id}`, formData, {
          responseType: "blob", //这里是声明期望返回的数据类型,为blob
        }).then(result => {
          console.log(result);
          // this.tableLoading = false;
          if (result) {
            // 创建一个包含结果数据的URL,并在URL末尾添加"#toolbar=0"来隐藏工具栏
            this.pageUrl = window.URL.createObjectURL(result.data) + "#toolbar=0";
            this.viewDlgTitle = '文件预览';
            this.viewDlg = true;
          }
        })
        this.$refs.uploadFile.clearFiles();
      } else {
        this.$message({
          message: "请上传格式为.pdf类型的文件",
          type: "warning",
        });
        this.tableLoading = false;
        this.$refs.uploadFile.clearFiles();
      }
    },
    // 下载
    downloadFile() {
      let fileName = this.currentRowData.name + ".pdf";
      if ("download" in document.createElement("a")) {
        let a = document.createElement("a");
        a.href = this.pageUrl;
        a.download = fileName;
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();
        URL.revokeObjectURL(a.href);
        document.body.removeChild(a);
      } else {
        navigator.msSaveBlob(blob, fileName);
      }
    },
    // 上传成功
    handleSuccess() {
      // this.tableLoading = false;
    },
    handleClick(row) {
      console.log(row);
      this.currentRowData = row;
      this.$refs.uploadFile.$children[0].$refs.input.click();
    }
  },

  data() {
    return {
      viewDlg: false,
      viewDlgTitle: '',
      pageUrl: '',
      currentRowData: {},
      diaLoading: false,
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333,
        id: '534a9b92-21fc-446f-9c99-2d123f927ed5'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1517 弄',
        zip: 200333,
        id: '111'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1519 弄',
        zip: 200333,
        id: '222'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1516 弄',
        zip: 200333,
        id: '333'
      }]
    }
  }
}
</script>

初版--01

相关推荐
华玥作者28 分钟前
[特殊字符] VitePress 对接 Algolia AI 问答(DocSearch + AI Search)完整实战(下)
前端·人工智能·ai
Mr Xu_1 小时前
告别冗长 switch-case:Vue 项目中基于映射表的优雅路由数据匹配方案
前端·javascript·vue.js
前端摸鱼匠1 小时前
Vue 3 的toRefs保持响应性:讲解toRefs在解构响应式对象时的作用
前端·javascript·vue.js·前端框架·ecmascript
lang201509281 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
好家伙VCC2 小时前
### WebRTC技术:实时通信的革新与实现####webRTC(Web Real-TimeComm
java·前端·python·webrtc
未来之窗软件服务3 小时前
未来之窗昭和仙君(六十五)Vue与跨地区多部门开发—东方仙盟练气
前端·javascript·vue.js·仙盟创梦ide·东方仙盟·昭和仙君
嘿起屁儿整3 小时前
面试点(网络层面)
前端·网络
VT.馒头3 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
phltxy4 小时前
Vue 核心特性实战指南:指令、样式绑定、计算属性与侦听器
前端·javascript·vue.js
Byron07075 小时前
Vue 中使用 Tiptap 富文本编辑器的完整指南
前端·javascript·vue.js