uploader.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <input
  3. ref="file"
  4. type="file"
  5. name="file"
  6. id
  7. style="display: none"
  8. :accept="accept"
  9. />
  10. </template>
  11. <script>
  12. import * as fileInfo from "@/utils/file";
  13. export default {
  14. props: {
  15. limit: Number,
  16. mediaType: Array,
  17. acceptType: String,
  18. failString: String,
  19. limitFailStr: String,
  20. },
  21. computed: {
  22. accept() {
  23. if (this.acceptType) {
  24. return this.acceptType;
  25. }
  26. if (!this.mediaType) {
  27. return '*'
  28. }
  29. let tmp = []
  30. this.mediaType.forEach(item => {
  31. tmp = tmp.concat(fileInfo.mediaMimes[item]
  32. .map((ii) => item + "/" + ii))
  33. });
  34. return tmp.join(",");
  35. },
  36. },
  37. mounted() {
  38. this.$el.addEventListener("change", (e) => {
  39. if (!window.FileReader) {
  40. this.$showAlert({
  41. tips:'无法上传'
  42. })
  43. e.target.value = "";
  44. return;
  45. }
  46. if (e.target.files.length === 0) {
  47. e.target.value = "";
  48. return;
  49. }
  50. this.files = e.target.files;
  51. this.files = Array.from(this.files).filter((item, i) => {
  52. if (this.mediaType.length > 0 ) {
  53. let pass = this.mediaType.find(jj=>{
  54. return fileInfo.checkMediaMime(jj, item.name)
  55. })
  56. if (!pass) {
  57. setTimeout(() => {
  58. if (this.failString) {
  59. this.$showAlert({
  60. tips:`“${item.name}”` + this.failString
  61. })
  62. } else {
  63. this.$showAlert({
  64. tips:"不支持该文件格式"
  65. })
  66. }
  67. }, i * 100);
  68. return;
  69. }
  70. }
  71. if (this.limit > 0) {
  72. if (!fileInfo.checkSizeLimitFree(item.size, this.limit)) {
  73. e.target.value = "";
  74. setTimeout(() => {
  75. if (this.limitFailStr) {
  76. this.$showAlert({
  77. tips:`“${item.name}”` + this.limitFailStr
  78. })
  79. }
  80. else{
  81. this.$showAlert({
  82. tips:`文件大小不能超过${this.limit}MB`
  83. })
  84. }
  85. }, i * 100);
  86. return;
  87. }
  88. } else if (!fileInfo.checkSizeLimit(this.mediaType, item.size)) {
  89. setTimeout(() => {
  90. if (this.limitFailStr) {
  91. this.$showAlert({
  92. tips:`“${item.name}”` + this.limitFailStr
  93. })
  94. }
  95. else{
  96. this.$showAlert({
  97. tips:`上传文件太大,不能超过${fileInfo.mediaMaxSize[this.mediaType]}MB`
  98. })
  99. }
  100. }, i * 100);
  101. return;
  102. }
  103. return item
  104. });
  105. this.$emit("file-change", this);
  106. e.target.value = "";
  107. });
  108. },
  109. beforeDestroy() {
  110. // this.clear();
  111. },
  112. methods: {
  113. click() {
  114. this.$el.click();
  115. },
  116. clear() {
  117. this.dataURL && window.URL.revokeObjectURL(this.dataURL);
  118. this.name = null;
  119. this.type = null;
  120. this.size = null;
  121. this.files = null;
  122. this.dataURL = null;
  123. this.base64 = null;
  124. this.poster = null;
  125. },
  126. upload() {},
  127. },
  128. };
  129. </script>