UploadFileDialog.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <el-dialog
  3. class="upload-file-dialog"
  4. :visible.sync="modalIsShow"
  5. title="上传文件"
  6. top="10vh"
  7. width="710px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. @opened="visibleChange"
  12. >
  13. <div class="file-upload-body">
  14. <upload-file-view
  15. input-width="270px"
  16. :format="format"
  17. :upload-url="uploadUrl"
  18. @valid-error="validError"
  19. @upload-success="uploadSuccess"
  20. ref="UploadFileView"
  21. ></upload-file-view>
  22. <el-button @click="toPreview" style="margin-left: 10px;">预览</el-button>
  23. </div>
  24. <div slot="footer" style="text-align: right">
  25. <el-button type="primary" @click="confirm">保存</el-button>
  26. <el-button type="danger" @click="cancel" plain>返回</el-button>
  27. </div>
  28. </el-dialog>
  29. </template>
  30. <script>
  31. import UploadFileView from "./UploadFileView";
  32. import { attachmentPreview } from "@/modules/login/api";
  33. export default {
  34. name: "upload-file-dialog",
  35. components: { UploadFileView },
  36. props: {
  37. paperAttachment: {
  38. type: Object,
  39. default() {
  40. return {};
  41. }
  42. },
  43. format: {
  44. type: Array,
  45. default() {
  46. return ["xls", "xlsx"];
  47. }
  48. }
  49. },
  50. data() {
  51. return {
  52. modalIsShow: false,
  53. attachment: {},
  54. // import
  55. uploadUrl: "/api/print/basic/sys/saveAttachment"
  56. };
  57. },
  58. methods: {
  59. visibleChange() {
  60. this.$refs.UploadFileView.setAttachmentName(
  61. `${this.paperAttachment.filename || ""}`
  62. );
  63. this.attachment = { ...this.paperAttachment };
  64. },
  65. // upload-handler
  66. validError(errorData) {
  67. this.$message.error(errorData.message);
  68. },
  69. uploadSuccess(data) {
  70. this.$message.success("上传成功!");
  71. let infos = {
  72. attachmentId: data.id,
  73. filename: data.filename
  74. };
  75. this.attachment = Object.assign(this.attachment, infos);
  76. },
  77. cancel() {
  78. this.modalIsShow = false;
  79. },
  80. open() {
  81. this.modalIsShow = true;
  82. },
  83. confirm() {
  84. this.$emit("confirm", this.attachment);
  85. this.cancel();
  86. },
  87. async toPreview() {
  88. if (!this.attachment.attachmentId) {
  89. this.$message.error("请先上传附件!");
  90. return;
  91. }
  92. const data = await attachmentPreview(this.attachment.attachmentId);
  93. window.open(data.url);
  94. }
  95. }
  96. };
  97. </script>
  98. <style lang="css" scoped>
  99. .file-upload-body {
  100. min-height: 150px;
  101. }
  102. </style>