vue使用iframe嵌入网页的示例代码

作者:w_t_y_y 时间:2024-05-05 09:12:04 

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("读取异常");
    }
}

来源:https://blog.csdn.net/w_t_y_y/article/details/84100658

标签:vue,iframe,嵌入网页
0
投稿

猜你喜欢

  • MySQL最常见的操作语句小结

    2023-12-27 19:33:56
  • 《色彩解答》系列之三 色彩对比

    2008-02-17 14:40:00
  • python实现二级登陆菜单及安装过程

    2023-09-16 16:05:44
  • Sql Server表死锁的解决方法分享

    2011-09-01 19:08:00
  • JavaScript解决Joseph问题

    2008-06-21 17:11:00
  • 如何卸载python插件

    2023-11-21 22:24:31
  • 详解python中各种文件打开模式

    2023-08-30 09:10:30
  • MSSQL数据库排序规则如何更改

    2023-07-01 11:09:58
  • tensorflow实现逻辑回归模型

    2022-01-18 20:10:28
  • Django的ListView超详细用法(含分页paginate)

    2021-02-13 00:53:41
  • 优化 MySQL 语句的十个建议

    2012-05-08 07:14:36
  • 1行Go代码实现反向代理的示例

    2024-04-28 09:15:26
  • Python3连接MySQL(pymysql)模拟转账实现代码

    2024-01-21 09:37:45
  • pytest接口测试之fixture传参数request的使用

    2023-03-19 07:40:40
  • python3第三方爬虫库BeautifulSoup4安装教程

    2023-05-10 21:11:57
  • 使用Python的Tornado框架实现一个简单的WebQQ机器人

    2023-03-19 03:26:09
  • 在 Windows 下搭建高效的 django 开发环境的详细教程

    2023-08-31 06:14:12
  • MySQL 4.0 升级到mysql 5.0的方法

    2024-01-18 10:07:12
  • 屏蔽Flash 右键菜单的方法

    2008-05-24 07:21:00
  • python通过百度地图API获取某地址的经纬度详解

    2021-11-06 12:14:45
  • asp之家 网络编程 m.aspxhome.com