You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.5 KiB

2 years ago
  1. # Gxd-vue-file-preview
  2. #### 介绍
  3. ```text
  4. vue 文件在线预览展示功能,支持文件(PDF,PNG,JPEG,JPG,GIF,DOC,DOCX,PPT,PPTX,ELXS,ELX)
  5. ```
  6. #### 预览
  7. ```markdown
  8. ![demo](http://static.e56buy.com/1610238353869.jpg "demo")
  9. ```
  10. #### 安装
  11. ```bash
  12. npm i gxd-file-preview --save --registry https://registry.npm.taobao.org
  13. ```
  14. #### 插件全局引用
  15. ``` javascript
  16. import vueFilePreview from 'gxd-file-preview';
  17. //初始化自定义插件,(pdf.js,worker.js文件建议放在本地服务器),cdn有可能不稳定
  18. Vue.use(vueFilePreview,{
  19. pdf: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.0.288/build/pdf.min.js', //pdf插件
  20. worker:'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.0.288/build/pdf.worker.min.js',//pdf.work插件
  21. });
  22. ```
  23. #### 插件使用
  24. ```vue
  25. <template>
  26. <div id="app"></div>
  27. </template>
  28. <script>
  29. export default {
  30. name: 'app',
  31. components: {},
  32. data(){
  33. return {}
  34. },
  35. created() {
  36. setTimeout(()=>{
  37. this.$preview({
  38. //url: 'https://testimg.tiangongy.com/100601/a024b86760bb1ff3b38f25ae2e0b9bdf', //图片
  39. //url: 'https://testimg.tiangongy.com/100601/9958ff80d202f91b347b14b5c56f14e8', // xlsx
  40. //url: 'https://testimg.tiangongy.com/100601/12d7e6a9b0b9169b800fbb29061212c2', //pptx
  41. //url: 'https://testimg.tiangongy.com/100601/ce44c69f3075334e6c624b8180a42804', //doc,
  42. //url: 'https://testimg.tiangongy.com/100601/3b85b4f1c3accdb4bb9f7e42e1f9070e',
  43. url:'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf',
  44. fid: '12121212'
  45. })
  46. }, 2000);
  47. },
  48. methods:{
  49. }
  50. }
  51. </script>
  52. <style>
  53. </style>
  54. ```
  55. ```vue
  56. <template>
  57. <div id="app">
  58. <h1>列表展示</h1>
  59. <xd-file-list-preview
  60. :show-close="showClose"
  61. :list="list" @remove="handleRemoveClick"
  62. :is-pagination="isPagination"
  63. ></xd-file-list-preview>
  64. <hr>
  65. <h1>文件预览模式</h1>
  66. <a @click="handleClick" style="color: #4285f4">9958ff80d202f91b347b14b5c56f14e811</a>
  67. </div>
  68. </template>
  69. <script>
  70. interface FileItemFormat {
  71. url:string; //文件路径(绝对路径)
  72. name?: string; //文件名称
  73. fid?: string; //文件ID
  74. download?: string; //预览页面是否显示下载按钮
  75. }
  76. export default {
  77. name: 'app',
  78. components: {},
  79. data() {
  80. return {
  81. showClose: true, //是否开启删除功能
  82. isPagination: true, //列表启动翻页功能
  83. /**@type FileItemFormat **/
  84. list: [
  85. {url: 'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf'},
  86. {url: 'https://jfb-public-images.oss-cn-qingdao.aliyuncs.com/admin-upload/202111081034429231.png?x-oss-process=style/common'},
  87. {url: 'http://static.e56buy.com/XdgfsqR2INp7uFxTuLQtnMstYLY4K8rr.蛋糕缺少内容.docx', name: 'aaaa'},
  88. {
  89. url: 'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf',
  90. name: 'aaaa',
  91. fid: 'aadadads',
  92. download: false ,//是否展示下载按钮
  93. },
  94. ]
  95. }
  96. },
  97. created() {
  98. },
  99. methods: {
  100. /**
  101. * @description 删除图片事件
  102. * @param item {Object} 当前被删除的文件对象
  103. * @param done {function} 删除文件完成回调函数
  104. */
  105. handleRemoveClick(item, done) {
  106. setTimeout(() => {
  107. console.log('handleRemoveClick', item);
  108. done()
  109. }, 2000);
  110. },
  111. /**
  112. * @description 点击查看预览功能
  113. */
  114. handleClick() {
  115. this.$preview({
  116. url: 'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf',
  117. fid: 'aadadads',
  118. download: false ,//是否展示下载按钮
  119. })
  120. },
  121. }
  122. }
  123. </script>
  124. <style>
  125. </style>
  126. ```
  127. ##### 项目开发下载与初始化
  128. ```bash
  129. # 克隆项目
  130. git clone git@gitee.com:e56buy/xd-file-preview.git
  131. # 进入项目目录
  132. cd xd-file-preview
  133. # 安装依赖
  134. npm install
  135. # 建议不要直接使用 cnpm 安装依赖,会有各种诡异的 bug。
  136. 建议通过npm按照,通过如下操作解决 npm 下载速度慢的问题
  137. npm install --save --registry=https://registry.npm.taobao.org
  138. ```
  139. ##### Nginx配置静态资源可以跨域访问(注意访问静态资源需要做跨域处理)
  140. ```text
  141. #全局模式
  142. server {
  143. listen 80;
  144. add_header 'Access-Control-Allow-Origin' '*';
  145. location /Roboto/ {
  146. root /home/images;
  147. autoindex on;
  148. }
  149. }
  150. #文件路径配置访问
  151. #访问路径拼接img访问本地绝对路径下的某图片
  152. location /img/ {
  153. #跨域配置,如果不生效请先清除浏览器缓存数据
  154. add_header 'Access-Control-Allow-Origin' '*';
  155. add_header 'Access-Control-Allow-Credentials' 'true';
  156. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  157. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  158. expires 30d;
  159. #当访问https://server_name/img/路径时,就会访问本的/Users/chokshen/Desktop/img/文件夹
  160. root /Users/chokshen/Desktop/;
  161. error_log off;
  162. access_log /dev/null;
  163. autoindex on;
  164. }
  165. ```
  166. ```text
  167. 版本日志
  168. 1.2.0
  169. 1、新增JSON,TXT,JS,HTML,CSS预览功能
  170. 2、列表展示功能新增 预览模式时候 左右翻页功能
  171. 1.1.18
  172. 优化文档显示
  173. 1.1.17
  174. 修复预览offic系列访问不成功问题
  175. ```