弹窗预览pdf,下载文档

<el-table-column label="操作">
	<template slot-scope="scope">
		<el-button type="text" size="mini" @click="handleSee(scope.$index, scope.row)">预览</el-button>
		<el-button size="mini" type="text" @click="downLoad(scope.$index, scope.row)">下载</el-button>
	</template>
</el-table-column>

 <el-dialog title="预览" :visible.sync="dialogVisible" width="90%" class="dialog">
      <iframe :src="iframeUrl" style="width: 100%; height: 83vh; overflow-y: auto; overflow-x: auto"></iframe>
 </el-dialog>
 
 data:{
    iframeUrl: "", //iframe预览显示
    dialogVisible: false, //弹出层是否显示
}
 // 预览
handleSee(index, row) {
	this.dialogVisible = true;
	yulan(row.id).then((res) => {
		if (this.dialogVisible == true) {
			var binaryData = [];
			binaryData.push(res);
			this.iframeUrl = window.URL.createObjectURL(
				new Blob(binaryData, { type: "application/pdf" })
			 );
		}
	});
},
// 下载
downLoad(index, row) {
	console.log(row);
	dodnLoadDoc(row.id).then((res) => {
          const url = URL.createObjectURL(res);
          const link = document.createElement("a");
          document.body.appendChild(link);
          link.download = row.reportName;
          link.href = url;
          link.click();
          document.body.removeChild(link);
          URL.revokeObjectURL(url);
        })
        .catch((error) => {
          this.$message({ message: error.message, type: "error" });
        });
    },
// 预览接口:
export function yulan(query) {
  return request({
    url: `XXX/${query}`,
    method: 'get',
    // params: query
    responseType: "blob",
  })
}
// 下载接口
export function dodnLoadDoc(query) {
  return request({
    url: `XXX/${query}`,
    method: 'get',
    // params: query
    responseType: "blob",
  })
}

你可能感兴趣的:(VUE,javascript,前端,开发语言)