3、vue实现pdf预览(本地文件地址转为url)

1、在iframe预览pdf标签中:

             <iframe
                :src="url"//绑定data中的url
                frameborder="0"
                width="100%"
                height="100%"
              ></iframe>

当地址来自本地上传的文件时需要加一个上传按钮,这里用element-ui里面的upload实现

<el-upload
              action="localhost.8080"
             //需要在script中定义httpReques函数,内容随便,哪怕是console.log(123),因为
              //:http-request代表的是覆盖掉上面的action行为,所以action里面的地址也可以乱填
              //,因 为定义了这个函数
              :http-request="httpRequest"  
              :show-file-list="false" //展不展示文件列表
              accept=".pdf" //表示接收文件类型
              :before-upload="beforeUpload" //这个函数下面第三部分有
            >
              <el-button type="primary">上传文件</el-button>
            </el-upload>

2、在data数据部分

url: "",

3、在script里面定义一个函数=>转本地地址为网络地址,从而实现预览

 beforeUpload(file, fileList) {
      if (window.createObjectURL != undefined) {
        this.url = window.createObjectURL(file);
      } else if (window.URL.createObjectURL(file)) {
        this.url = window.URL.createObjectURL(file);
      } else if (window.webkitURL != undefined) {
        this.url = window.webkitURL.createObjectURL(file);
      }
      console.log(this.url);
    },

4、运行效果图

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值