美文网首页
二进制流实现pdf预览

二进制流实现pdf预览

作者: Taijia | 来源:发表于2021-07-07 09:26 被阅读0次
    <template>
      <div v-loading="loading" style="height: 100%">
        <iframe
          :src="url"
          width="100%"
          height="100%"
          frameborder="0"
        />
    
      </div>
    </template>
    
    <script>
    export default {
      data: vm => ({
        loading: false,
        documentId: vm.$route.query.documentId,
        url: ''
      }),
      created () {
        this.previewDocument();
        // 防止出现两个滚动条(body和iframe)
        const body = document.getElementsByTagName('body')[0];
        body.style.overflow = 'hidden';
        // iframe 占满整个页面
        const app = document.getElementById('app');
        app.style.height = '100%';
      },
      methods: {
        previewDocument () {
          this.loading = true;
          this.$http({
            url: '/rssapi/RentCollection/DownloadDocument',
            params: { documentIds: this.documentId },
            responseType: 'arraybuffer'
          })
            .then(res => {
              const blob = new Blob([res], {
                type: 'application/pdf'
              });
              this.url = window.URL.createObjectURL(blob);
            })
            .catch(e => {
              this.$message.error(`${e.code}: ${e.message}`);
            })
            .finally(() => {
              this.loading = false;
            });
        }
      }
    };
    </script>
    
    

    相关文章

      网友评论

          本文标题:二进制流实现pdf预览

          本文链接:https://www.haomeiwen.com/subject/hhhoultx.html