UploadFile.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <input ref="file" type="file" name="file" id style="display: none" />
  3. </template>
  4. <script>
  5. import * as fileInfo from "../../utils/file";
  6. export default {
  7. props: {
  8. mediaType: String,
  9. },
  10. data() {
  11. return {
  12. ext: "",
  13. name: "",
  14. type: "",
  15. size: 0,
  16. };
  17. },
  18. mounted() {
  19. this.$el.addEventListener("change", (e) => {
  20. if (!window.FileReader) {
  21. this.$confirm({ content: this.$t("common.uploads.cant_upload") });
  22. return;
  23. }
  24. if (e.target.files.length === 0) {
  25. this.ext = "";
  26. this.name = "";
  27. this.type = "";
  28. this.size = 0;
  29. this.dataURL = "";
  30. return;
  31. }
  32. const file = e.target.files[0];
  33. if (!fileInfo.checkMediaMime(this.mediaType, file.name)) {
  34. return this.$confirm({
  35. content: this.$t("common.uploads.not_support", {
  36. fileType: fileInfo.mediaTypes[this.mediaType],
  37. }),
  38. });
  39. }
  40. if (!fileInfo.checkSizeLimit(this.mediaType, file.size)) {
  41. return this.$confirm({
  42. content: this.$t("common.uploads.not_support", {
  43. fileType: fileInfo.mediaMaxSize[this.mediaType],
  44. }),
  45. });
  46. }
  47. const reader = new FileReader();
  48. reader.readAsDataURL(file);
  49. reader.onload = (e) => {
  50. this.dataURL && window.URL.revokeObjectURL(this.dataURL);
  51. this.name = file.name;
  52. this.type = file.type;
  53. this.mime = fileInfo.getMime(file.name);
  54. this.size = file.size;
  55. this.dataURL = fileInfo.base64ToDataURL(e.target.result);
  56. this.$emit("file-change", this, e);
  57. };
  58. e.target.value = "";
  59. });
  60. },
  61. methods: {
  62. click() {
  63. this.$el.click();
  64. },
  65. upload() {},
  66. },
  67. };
  68. </script>