index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <div>
  3. <div v-if="fileId" class="pdf-css">
  4. <el-image
  5. @click="check(fileId)"
  6. :src="
  7. accept === '.pdf'
  8. ? require('@/assets/PDF.png')
  9. : (accept.indexOf('.jpg') !== -1 ||
  10. accept.indexOf('.png') !== -1 ||
  11. accept.indexOf('.jpeg') !== -1) &&
  12. accept.indexOf('.pdf') == -1 &&
  13. accept.indexOf('.xlsx') == -1
  14. ? require('@/assets/image.png')
  15. : require('@/assets/doc.png')
  16. "
  17. alt
  18. :preview-src-list="srcList"
  19. ></el-image>
  20. <i
  21. v-if="!readonly"
  22. class="el-icon-error fork"
  23. @click="deltabpdf(fileId)"
  24. ></i>
  25. </div>
  26. <el-upload
  27. v-loading="loading"
  28. v-else
  29. action=""
  30. :on-change="handleChange"
  31. :show-file-list="false"
  32. :file-list="fileList"
  33. :accept="accept"
  34. :auto-upload="false"
  35. ref="upload"
  36. class="upload-css"
  37. >
  38. <el-button type="primary" :disabled="canUp">点击上传</el-button>
  39. <div slot="tip" class="el-upload__tip"><slot name="dec"></slot></div>
  40. </el-upload>
  41. <el-dialog
  42. title="预览"
  43. :visible.sync="dialogFile"
  44. width="70%"
  45. append-to-body
  46. >
  47. <iframe
  48. :src="pdfSrc"
  49. frameborder="0"
  50. style="width: 100%; height: 100%"
  51. ></iframe>
  52. </el-dialog>
  53. </div>
  54. </template>
  55. <script>
  56. import { uploadData, uploadCheck } from "@/api/user";
  57. export default {
  58. name: "ylUpload",
  59. props: {
  60. // 对话框('dialog')还是跳页面('window')打开文件
  61. openFileType: {
  62. type: String,
  63. default: "dialog"
  64. },
  65. // 上传路径名称,用于文件夹分类,方便查找
  66. urlName: {
  67. type: String,
  68. default: "spd"
  69. },
  70. // 文档类别,.pdf, .jpg等
  71. accept: {
  72. type: String,
  73. default: ".pdf"
  74. },
  75. // 最大M数,上传的大小
  76. maxSize: {
  77. type: Number,
  78. default: 10
  79. },
  80. // 传递的id值
  81. fileId: {
  82. type: [Number, String],
  83. default: ""
  84. },
  85. // 文件是否只读
  86. readonly: {
  87. type: Boolean,
  88. default: false
  89. },
  90. index:{
  91. type: Number,
  92. default: 0,
  93. }
  94. },
  95. data() {
  96. return {
  97. loading: false,
  98. // pdf
  99. dialogFile: false,
  100. pdfSrc: "",
  101. srcList: [""],
  102. type: ""
  103. };
  104. },
  105. methods: {
  106. // 上传文件
  107. handleChange(file) {
  108. this.loading = true;
  109. const max = this.maxSize * 1024 * 1024;
  110. if (file.size > max) {
  111. this.$message({
  112. message: "上传文件大小不能超过" + this.maxSize + "M!",
  113. type: "warning"
  114. });
  115. this.loading = false;
  116. return false;
  117. } else if (file.name.indexOf(",") !== -1) {
  118. this.$message.warning("文件名称不能存在英文逗号");
  119. this.loading = false;
  120. return false;
  121. } else if (this.accept && this.accept.indexOf("/") === -1) {
  122. const suffix = file.name.substring(file.name.lastIndexOf(".") + 1);
  123. const acceptArr = this.accept
  124. .replace(/\./g, "")
  125. .toLowerCase()
  126. .split(",");
  127. if (!acceptArr.includes(suffix)) {
  128. this.$message.warning(`仅支持上传${acceptArr.join("、")}格式文件`);
  129. this.loading = false;
  130. return false;
  131. }
  132. }
  133. const reader = new FileReader();
  134. reader.readAsDataURL(file.raw);
  135. reader.onload = async (e) => {
  136. console.log(e);
  137. const fileCode = e.target.result;
  138. const data = {
  139. fileName: file.name,
  140. itemcode: this.urlName,
  141. fileContent: fileCode
  142. };
  143. const res = await uploadData(data, this);
  144. if (res.success == true) {
  145. this.$emit("getUpload", res.data.id,this.index);
  146. this.loading = false;
  147. } else {
  148. this.loading = false;
  149. }
  150. };
  151. },
  152. // 表单点击预览
  153. check(id) {
  154. this.pdfSrc = "";
  155. this.srcList = false;
  156. this.type = "";
  157. let file = {
  158. id: id
  159. };
  160. uploadCheck(file).then((res) => {
  161. if (res.success == true) {
  162. const a = res.data.fileContent;
  163. this.type = res.data.fileSufx;
  164. // 打开pdf
  165. if (res.data.fileSufx === "pdf") {
  166. console.log(222)
  167. const b = this.dataURLtoBlob(res.data.fileContent.slice(28));
  168. console.log(b,"bb")
  169. if (this.openFileType === "dialog") {
  170. this.dialogFile = true;
  171. this.pdfSrc = b;
  172. } else if (this.openFileType === "window") {
  173. window.open(b);
  174. }
  175. } else if (
  176. this.type == "jpg" ||
  177. this.type == "png" ||
  178. this.type.toLowerCase() == "jpeg"
  179. ) {
  180. //打开图片
  181. this.srcList = [a];
  182. }
  183. }
  184. });
  185. },
  186. // 预览文件
  187. dataURLtoBlob(e) {
  188. console.log(111)
  189. let bstr = atob(e);
  190. let n = bstr.length;
  191. let u8arr = new Uint8Array(n);
  192. while (n--) {
  193. u8arr[n] = bstr.charCodeAt(n);
  194. } //确定解析格式
  195. let blob = new Blob([u8arr], { type: "application/pdf;chartset=UTF-8" });
  196. let url = window.URL.createObjectURL(blob);
  197. return url;
  198. },
  199. deltabpdf(id) {
  200. this.$emit("getUpload", "",this.index);
  201. }
  202. }
  203. };
  204. </script>
  205. <style scoped lang="scss">
  206. .pdf-css {
  207. position: relative;
  208. width: 42px;
  209. height: 42px;
  210. padding-top: 4px;
  211. padding-left: 10px;
  212. img {
  213. width: 35px;
  214. height: 35px;
  215. }
  216. .fork {
  217. cursor: pointer;
  218. position: absolute;
  219. top: 4px;
  220. right: -11px;
  221. color: #cecece;
  222. }
  223. }
  224. .upload-css {
  225. padding: 5px;
  226. }
  227. ::v-deep .el-loading-mask {
  228. right: auto;
  229. left: 26px;
  230. }
  231. ::v-deep .el-dialog {
  232. height: 90%;
  233. margin-top: 30px !important;
  234. }
  235. ::v-deep .el-dialog__body {
  236. height: 90%;
  237. padding: 25px 25px 0px;
  238. }
  239. </style>