UploadWidget.vue 1.3 KB

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