(八)vue使用iframe嵌入网页

1、列表页面:

 this.$router.push({ name: 'userTemplate', params: { reportUrl: reportUrl, reportType: reportType }})
     

 点击查看后触发事件,带入参数跳转到userTemplate页面;reportType有两种类型,0表示reportUrl是绝对网址,1表示reportUrl是本地html文件。

2、userTemplate页面:

<template>
 <div> 
   <iframe v-if="reportType==0" name = "child" id = "child" v-bind:src="reportUrl"
   width="auto" height="300"
     frameborder="0" scrolling="no"  style="position:absolute;top:80px;left: 30px;"
    ></iframe>

 <div v-if="reportType==1" v-html="htmlContent"
 width="auto" height="300"  scrolling="no"  style="position:absolute;top:80px;left: 30px;"></div>

 </div>
</template>

<script>
import {
  getFile
} from '@/api/report'
export default {
  mounted() {
    /**
      * iframe-宽高自适应显示
      */
    function changeMobsfIframe() {
      const mobsf = document.getElementById('child')
      const deviceWidth = document.body.clientWidth
      const deviceHeight = document.body.clientHeight
      mobsf.style.width = (Number(deviceWidth) - 30) + 'px' // 数字是页面布局宽度差值
      mobsf.style.height = (Number(deviceHeight) - 80) + 'px' // 数字是页面布局高度差
    }

    changeMobsfIframe()

    window.onresize = function() {
      changeMobsfIframe()
    }
  },

  data() {
    return {
      htmlContent: '',
      reportUrl: '',
      reportType: ''
    }
  },
  created() {
    // this.fileName = '../static/file/' + this.$route.params.report_url
    this.reportUrl = this.$route.params.reportUrl
    this.reportType = this.$route.params.reportType
    if (this.reportType == 1) {
      getFile(this.reportUrl).then(res => {
        if (res.code === 200) {
          this.htmlContent = res.data
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
 
</style>

后端getFile:

//读取文件
	@RequestMapping("/getFile")
    @ResponseBody
	public HttpResult getFile(String reportUrl){
		 StringBuilder result = new StringBuilder();
	        try{
	        	
	        	FileSystemResource resource = new FileSystemResource("D:"+File.separator+"test"+File.separator+reportUrl);
	        	File file = resource.getFile();
	            BufferedReader br = new BufferedReader(new FileReader(file));
	            String s = null;
	            while((s = br.readLine())!=null){
	                result.append(System.lineSeparator()+s);
	            }
	            br.close();   
	            return HttpResult.getSuccessInstance(result);
	        }catch(Exception e){
	            e.printStackTrace();
	            return HttpResult.getFailedInstance("读取异常");
	        }
			

	}

如果希望覆盖整个页面:

 <iframe 
      class="child"
      v-bind:src="url"
      :style="{width: width, height:height}"
    ></iframe>



 data() {
    return {
      url:'',
      height: window.innerHeight + 'px',
      width: '100%'
    };
  },

 

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值