First, request needs to be modified
responseType: ‘blob’, need to be modified
Request header { responseType: 'blob', url: url, method: 'get', }
Three methods:
1. Process it directly and open it on a new page
const blob = new Blob([data],{ type:'application/pdf' }) let url = (blob) (url,'_blank') The problem is that the parent page is closed or refreshed,File page cannot obtain file stream,Refresh display blank page。
2. Use iframe to connect on the new page
<iframe :src='xxxxxx'> The problem is clickiframeThe file cannot be stored afteriframeListen to events,ctrl+p Show blank
3. Use
arrive //Gett… Find the download location on the page, downloadexist Revise Comment the following code otherwise Cross-domain errors may occur,Unable to preview the file normally if (origin !== viewerOrigin && protocol !== "blob:") { throw new Error("file origin does not match viewer's"); } 随后exist页面展示 let path = (blob) const fileUrl = '/pdfjs2/web/' // In production environmentif (.NODE_ENV === 'production') { = fileUrl + '?file=' + encodeURIComponent(path) } else { // Development environment = fileUrl + '?file=' + encodeURIComponent(path) } Revise清晰度 --Pay attention to the higher the clarity,When printing preview Google kernel scrollbar stuck this._printResolution = 450//printResolution || 150 New version of Have been changed tomjs,When onlinenginx需要Revise There is anotherbug exist一个页面When printing preview,Other pages with the same origin cannot be clicked
Attachment: Blob stream online preview PDF files and pictures
Pay attention to the format and addresponseType: 'arraybuffer'
import axios from 'axios' const fileTypeList = ['application/pdf', 'image/png', 'image/gif', 'image/jpeg', 'txt/plain'] invoicePreview () { axios({ method: 'get', url: '/acc_test/index/test_pdf', baseURL: , responseType: 'arraybuffer' }).then(res => { let fileType = ['content-type'] const binaryData = [] if (fileType && (fileType)) { () let URL = (new Blob(binaryData, { type: fileType, charset: 'utf-8' })) (URL) } else { this.$('This file preview is not supported') } }) }
Summarize
This is the end of this article about three methods of displaying pdf on the front end of the blob file stream. For more related blob file stream display pdf content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!