欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用iframe實(shí)現(xiàn)pdf文件預(yù)覽功能

 更新時(shí)間:2023年09月07日 11:05:21   作者:前端不加班  
這篇文章主要為大家詳細(xì)介紹了如何使用iframe實(shí)現(xiàn)pdf文件預(yù)覽功能,以及iframe預(yù)覽報(bào)錯(cuò)問(wèn)題和iframe未能加載PDF文檔,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本文主要介紹使用iframe預(yù)覽pdf的功能,以及iframe預(yù)覽報(bào)錯(cuò)問(wèn)題和iframe未能加載PDF文檔。

預(yù)覽自帶分頁(yè)、下載、旋轉(zhuǎn)、比例等功能。

一、iframe是什么

iframe的介紹:將src的內(nèi)容規(guī)定在 中顯示出。

iframe既可以用來(lái)預(yù)覽本地static下的文檔,也可以預(yù)覽后端返回的文件流文檔

二、使用步驟

1.使用與邏輯

邏輯:就是將后端返回的看不懂的文件流,設(shè)置成responseType = 'blob'然后讀取到返回的Blob,再使用createObjectURL讀取出url即可對(duì)于后綴名是大寫(xiě)的.PDF 沒(méi)法直接預(yù)覽,建議直接下載下來(lái)看

 <iframe  :src="url" style="border: none;width: 100%;height: 100%;">
     <p>您的瀏覽器不支持 iframe 標(biāo)簽,請(qǐng)從列表中下載預(yù)覽</p>
 </iframe>
  data () {
    return {
      url: '',
      }
  },
  methods: {
   downLoadFileImg (fileUrl, fileName) {// 后端文件地址和名稱
      // 可下載,名稱也有效 -- 推薦
      const x = new window.XMLHttpRequest()
      x.open('GET', fileUrl, true)
      x.responseType = 'blob' // 選擇返回格式為blob --- 一般后端返回的是看不懂的文件流 故需要轉(zhuǎn)成blob
      x.onload = () => {
        this.url = window.URL.createObjectURL(x.response) //將后端返回的blob文件讀取出url
		console.log('blob====',x.response)   //Blob {size: 38617, type: 'application/pdf'}
		console.log('url====',this.url)   //blob:http://localhost:7197/cb047277-e5e6-4905-bf8c-dbecd86a0105
		// url可以預(yù)覽和下載------如果想要下載就把下方注釋打開(kāi)即可
        // const a = document.createElement('a')
        // a.href = this.url
        // a.download = fileName
        // a.click()
      }
      x.send()
      // // 或者下方的方式
      // axios({
      //   url: G_CGI_PHP.superOrder.upfile + "?id=" + this.rowData.id,
      //   method: 'get',
      //   timeout: 100000,
      //   responseType: 'blob',
      //   headers: {
      //     // "type": 'application/pdf'
      //   },
      // }).then(res => {
      //   console.log('res.data', res.data)
      //   this.url = window.URL.createObjectURL(res.data) //將后端返回的blob文件讀取出url
      //   console.log('通過(guò)讀取blob文件得到url地址===', this.url)
      // }).catch(error => {
      //   console.log(error)
      // })
    },
  }

2.圖文詳解

3.錯(cuò)誤情況

原因:說(shuō)明你createObjectURL讀的不是設(shè)置responseType: 'blob’后獲取的Blob,可能是直接讀的后端返回的那個(gè)看不懂的文件流res

到此這篇關(guān)于使用iframe實(shí)現(xiàn)pdf文件預(yù)覽功能的文章就介紹到這了,更多相關(guān)iframe pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論