|
2 | 2 | * @Author: zhanghan
|
3 | 3 | * @Date: 2023-01-10 14:28:29
|
4 | 4 | * @LastEditors: zhanghan
|
5 |
| - * @LastEditTime: 2023-01-10 20:03:38 |
| 5 | + * @LastEditTime: 2023-01-11 11:45:47 |
6 | 6 | * @Descripttion: 首页
|
7 | 7 | -->
|
8 | 8 | <template>
|
9 | 9 | <div class="page-home page">
|
10 |
| - <h2>iframe方式示例:</h2> |
11 |
| - <h4>推荐独立部署{{ config.name }},并使用iframe方式引入以减少体积</h4> |
12 |
| - <h4> |
13 |
| - 1、需要自行前往本项目源码处下载编译后的产物 |
14 |
| - <a |
15 |
| - href="https://github.com/zyl-ui/vue-file-viewer/tree/master/public/file-viewer" |
16 |
| - target="_blank" |
17 |
| - > |
18 |
| - file-viewer |
19 |
| - </a> |
20 |
| - <br /> |
21 |
| - (必须包含file-viewer文件夹,不可重命名,若需要重命名可下载源码重新修改构建) |
22 |
| - </h4> |
23 |
| - <h4>2、下载后放在项目public文件夹中作为外部公共资源使用</h4> |
24 |
| - <h4>* 本项目示例链接随时失效,请勿直接用于生产环境</h4> |
25 |
| - <h4>* 被浏览的文件链接必须同源或本身支持跨域访问</h4> |
26 |
| - <br /> |
27 |
| - |
28 |
| - <h4>一般url传入使用</h4> |
29 | 10 | <section class="demo">
|
30 | 11 | <div
|
31 | 12 | class="section-content swiper"
|
32 |
| - style="height: 500px;overflow: hidden;" |
| 13 | + style="height: 1000px;overflow: hidden;" |
33 | 14 | >
|
34 | 15 | <iframe
|
35 |
| - src="/file-viewer/index.html?fileUrl=https://home.sharecorner.top/fileTest/pdf.pdf" |
36 | 16 | scrolling="auto"
|
| 17 | + seamless |
| 18 | + src="/file-viewer/index.html?shoHead=true&&fileUrl=https://home.sharecorner.top/fileTest/pdf.pdf" |
37 | 19 | style="border:0;height: 100%;width:100%"
|
38 | 20 | />
|
39 | 21 | </div>
|
40 | 22 | </section>
|
41 |
| - <section class="snippets"> |
42 |
| - <Collapse> |
43 |
| - <div class="section-content"> |
44 |
| - <CodeSnippet class="snippet" :code="iframeSnippet" lang="html" /> |
45 |
| - </div> |
46 |
| - </Collapse> |
47 |
| - </section> |
48 |
| - |
49 |
| - <h4>支持二进制文件流推送</h4> |
50 |
| - <section class="demo"> |
51 |
| - <div |
52 |
| - class="section-content swiper" |
53 |
| - style="height: 500px;overflow: hidden;" |
54 |
| - id="blobIframe" |
55 |
| - ></div> |
56 |
| - </section> |
57 |
| - <section class="snippets"> |
58 |
| - <Collapse> |
59 |
| - <div class="section-content"> |
60 |
| - <CodeSnippet class="snippet" :code="blobIframeSnippet1" lang="html" /> |
61 |
| - <div class="plus">+</div> |
62 |
| - <CodeSnippet class="snippet" :code="blobIframeSnippet2" lang="js" /> |
63 |
| - </div> |
64 |
| - </Collapse> |
65 |
| - </section> |
66 |
| - |
67 |
| - <h2>组件方式示例:</h2> |
68 |
| - <h4>安装:</h4> |
69 |
| - <CodeSnippet class="snippet" :code="installSnippet" lang="js" /> |
70 |
| - <h4>使用:</h4> |
71 |
| - <section class="demo"> |
72 |
| - <div class="section-content swiper" style="height: 500px;"> |
73 |
| - <vue-file-viewer :file="file" /> |
74 |
| - </div> |
75 |
| - </section> |
76 |
| - <section class="snippets"> |
77 |
| - <Collapse> |
78 |
| - <div class="section-content"> |
79 |
| - <CodeSnippet class="snippet" :code="componentSnippet1" lang="html" /> |
80 |
| - <div class="plus">+</div> |
81 |
| - <CodeSnippet class="snippet" :code="componentSnippet2" lang="js" /> |
82 |
| - </div> |
83 |
| - </Collapse> |
84 |
| - </section> |
85 | 23 | </div>
|
86 | 24 | </template>
|
87 | 25 |
|
88 | 26 | <script>
|
89 |
| -import axios from 'axios' |
90 |
| -import CodeSnippet from '../components/CodeSnippet.vue' |
91 |
| -import Collapse from '../components/Collapse.vue' |
92 |
| -import { config } from '../config' |
93 |
| - |
94 |
| -const iframeSnippet = ` |
95 |
| -<iframe |
96 |
| - src="/file-viewer/index.html?fileUrl=https://home.sharecorner.top/fileTest/pdf.pdf" |
97 |
| - scrolling="auto" |
98 |
| - style="border:0;height: 100%;width:100%" |
99 |
| -/> |
100 |
| -` |
101 |
| - |
102 |
| -const blobIframeSnippet1 = ` |
103 |
| -<div id="blobIframe"></div> |
104 |
| -` |
105 |
| - |
106 |
| -const blobIframeSnippet2 = ` |
107 |
| -import axios from 'axios' |
108 |
| -export default { |
109 |
| - data() { |
110 |
| - return { |
111 |
| - context: { |
112 |
| - // 查看器的地址 |
113 |
| - // 自己项目内部署需要将编译后的产物file-viewer放在public文件夹中使用,编译后的产物需要下载,下载链接在文档上 |
114 |
| - origin: location.origin + '/file-viewer/index.html', |
115 |
| - // 目标frame |
116 |
| - frame: null, |
117 |
| - // 浏览的文件url |
118 |
| - url: 'https://home.sharecorner.top/fileTest/pic.png' |
119 |
| - } |
120 |
| - } |
121 |
| - }, |
122 |
| - mounted() { |
123 |
| - this.loadFromUrl() |
124 |
| - }, |
125 |
| - methods: { |
126 |
| - // 获取文件二进制流 |
127 |
| - loadFromUrl() { |
128 |
| - // 要预览的文件地址 |
129 |
| - var url = this.context.url |
130 |
| - // 取得文件名 |
131 |
| - var filename = url.substring(url.lastIndexOf('/') + 1) |
132 |
| - // 拼接iframe请求url |
133 |
| - var src = |
134 |
| - this.context.origin + |
135 |
| - '?name=' + |
136 |
| - encodeURIComponent(filename) + |
137 |
| - '&from=' + |
138 |
| - encodeURIComponent(location.origin) |
139 |
| - // 拼接frame |
140 |
| - var frame = this.appendFrame(src) |
141 |
| - // 绑定事件 |
142 |
| - frame.onload = () => { |
143 |
| - axios({ |
144 |
| - url, |
145 |
| - method: 'get', |
146 |
| - responseType: 'blob' |
147 |
| - }).then((data) => { |
148 |
| - if (!data) { |
149 |
| - console.error('文件下载失败') |
150 |
| - } |
151 |
| - console.log(data) |
152 |
| - // 最重要的一步,可以推送流到iframe |
153 |
| - frame.contentWindow.postMessage(data.data, this.context.origin) |
154 |
| - }) |
155 |
| - } |
156 |
| - }, |
157 |
| - // 插入iframe |
158 |
| - appendFrame(src) { |
159 |
| - var blobIframe = document.getElementById('blobIframe') |
160 |
| - if (this.context.frame) { |
161 |
| - blobIframe.removeChild(this.context.frame) |
162 |
| - } |
163 |
| - // 构建frame |
164 |
| - var frame = (this.context.frame = document.createElement('iframe')) |
165 |
| - frame.src = src |
166 |
| - frame.style = 'border:0;height: 100%;width:100%' |
167 |
| - return blobIframe.appendChild(frame) |
168 |
| - } |
169 |
| - } |
170 |
| -` |
171 |
| - |
172 |
| -const installSnippet = ` |
173 |
| -import Vue from 'vue' |
174 |
| -import App from './App.vue' |
175 |
| -import vueFileViewer from '${config.name}' |
176 |
| - |
177 |
| -Vue.use(vueFileViewer) |
178 |
| - |
179 |
| -new Vue({ |
180 |
| - el: '#app', |
181 |
| - render: h => h(App) |
182 |
| -}); |
183 |
| -` |
184 |
| - |
185 |
| -const componentSnippet2 = ` |
186 |
| -data () { |
187 |
| - return { |
188 |
| - file: 'https://home.sharecorner.top/fileTest/pdf.pdf' // or 原生 File 文件对象也可以被接收 |
189 |
| - } |
190 |
| -} |
191 |
| -` |
192 |
| - |
193 |
| -const componentSnippet1 = ` |
194 |
| -<vue-file-viewer :file="file" /> |
195 |
| -` |
196 | 27 | export default {
|
197 | 28 | name: 'Home',
|
198 |
| - components: { |
199 |
| - CodeSnippet, |
200 |
| - Collapse |
201 |
| - }, |
202 | 29 |
|
203 | 30 | data() {
|
204 |
| - return { |
205 |
| - config, |
206 |
| - iframeSnippet, |
207 |
| - blobIframeSnippet1, |
208 |
| - blobIframeSnippet2, |
209 |
| - installSnippet, |
210 |
| - componentSnippet2, |
211 |
| - componentSnippet1, |
212 |
| - file: 'https://home.sharecorner.top/fileTest/word.docx', |
213 |
| - context: { |
214 |
| - // 查看器的地址 |
215 |
| - // 自己项目内部署需要将编译后的产物file-viewer放在public文件夹中使用,编译后的产物需要下载,下载链接在文档上 |
216 |
| - origin: location.origin + '/file-viewer/index.html', |
217 |
| - // 目标frame |
218 |
| - frame: null, |
219 |
| - // 浏览的文件url |
220 |
| - url: 'https://home.sharecorner.top/fileTest/pic.png' |
221 |
| - } |
222 |
| - } |
223 |
| - }, |
224 |
| - mounted() { |
225 |
| - this.loadFromUrl() |
| 31 | + return {} |
226 | 32 | },
|
227 |
| - methods: { |
228 |
| - // 获取文件二进制流 |
229 |
| - loadFromUrl() { |
230 |
| - // 要预览的文件地址 |
231 |
| - var url = this.context.url |
232 |
| - // 取得文件名 |
233 |
| - var filename = url.substring(url.lastIndexOf('/') + 1) |
234 |
| - // 拼接iframe请求url |
235 |
| - var src = |
236 |
| - this.context.origin + |
237 |
| - '?name=' + |
238 |
| - encodeURIComponent(filename) + |
239 |
| - '&from=' + |
240 |
| - encodeURIComponent(location.origin) |
241 |
| - // 拼接frame |
242 |
| - var frame = this.appendFrame(src) |
243 |
| - // 绑定事件 |
244 |
| - frame.onload = () => { |
245 |
| - axios({ |
246 |
| - url, |
247 |
| - method: 'get', |
248 |
| - responseType: 'blob' |
249 |
| - }).then((data) => { |
250 |
| - if (!data) { |
251 |
| - console.error('文件下载失败') |
252 |
| - } |
253 |
| - console.log(data) |
254 |
| - // 最重要的一步,可以推送流到iframe |
255 |
| - frame.contentWindow.postMessage(data.data, this.context.origin) |
256 |
| - }) |
257 |
| - } |
258 |
| - }, |
259 |
| - // 插入iframe |
260 |
| - appendFrame(src) { |
261 |
| - var blobIframe = document.getElementById('blobIframe') |
262 |
| - if (this.context.frame) { |
263 |
| - blobIframe.removeChild(this.context.frame) |
264 |
| - } |
265 |
| - // 构建frame |
266 |
| - var frame = (this.context.frame = document.createElement('iframe')) |
267 |
| - frame.src = src |
268 |
| - frame.style = 'border:0;height: 100%;width:100%' |
269 |
| - return blobIframe.appendChild(frame) |
270 |
| - } |
271 |
| - } |
| 33 | + methods: {} |
272 | 34 | }
|
273 | 35 | </script>
|
274 | 36 |
|
|
0 commit comments