UploadWidget.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div>
  3. <input
  4. ref="fileInput"
  5. type="file"
  6. class="form-control file-input"
  7. :accept="fileType"
  8. @change="onFileChange"
  9. />
  10. <label for="attachment">
  11. <a
  12. role="button"
  13. class="btn btn-primary btn-sm"
  14. @click="clickButton"
  15. >
  16. <i class="fa fa-upload" />
  17. &nbsp;
  18. {{ $t('lang.uploadWidget.uploadFile') }}
  19. </a>
  20. </label>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. name: 'UploadWidget',
  26. /**
  27. * fileType: 上传文件类型,eg:".xls,.doc,.txt,.pdf"
  28. */
  29. props: {
  30. 'fileType': {
  31. type: String,
  32. default: '',
  33. },
  34. },
  35. emits: [ 'upload' ],
  36. data: function () {
  37. return {
  38. };
  39. },
  40. methods: {
  41. /**
  42. * 点击上传按钮事件
  43. * @return {[type]} [description]
  44. */
  45. clickButton: function () {
  46. $(this.$refs.fileInput).click();
  47. },
  48. /**
  49. * 选择了文件事件
  50. * @return {[type]} [description]
  51. */
  52. onFileChange: function (e) {
  53. var files = e.target.files || e.dataTransfer.files;
  54. if (!files.length)
  55. return;
  56. this.$emit('upload', files);
  57. },
  58. },
  59. };
  60. </script>
  61. <style scoped>
  62. .file-input {
  63. width: 0.1px;
  64. height: 0.1px;
  65. opacity: 0;
  66. overflow: hidden;
  67. position: absolute;
  68. z-index: -1;
  69. }
  70. </style>