Upload.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <Upload
  3. name="files"
  4. ref="upload"
  5. :action="actionUrl"
  6. :data="params"
  7. multiple
  8. :format="uploadFormat"
  9. :show-upload-list="false"
  10. :max-size="maxSize"
  11. :on-progress="handleProgress"
  12. :on-success="handleSuccess"
  13. :on-format-error="handleFormatError"
  14. :on-exceeded-size="handleMaxSize"
  15. :before-upload="handleBeforeUpload">
  16. </Upload>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'ChatLoad',
  21. props: {
  22. target: {
  23. default: ''
  24. },
  25. maxSize: {
  26. type: Number,
  27. default: 204800
  28. }
  29. },
  30. data() {
  31. return {
  32. uploadFormat: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'],
  33. actionUrl: $A.apiUrl('chat/files/upload'),
  34. params: {
  35. username: this.target,
  36. token: $A.getToken(),
  37. }
  38. }
  39. },
  40. watch: {
  41. target(val) {
  42. this.$set(this.params, 'username', val);
  43. }
  44. },
  45. methods: {
  46. handleProgress(event, file) {
  47. //上传时
  48. if (typeof file.tempId === "undefined") {
  49. file.tempId = $A.randomString(8);
  50. this.$emit('on-progress', file);
  51. }
  52. },
  53. handleSuccess(res, file) {
  54. //上传完成
  55. if (res.ret === 1) {
  56. for (let key in res.data) {
  57. if (res.data.hasOwnProperty(key)) {
  58. file[key] = res.data[key];
  59. }
  60. }
  61. this.$emit('on-success', file);
  62. } else {
  63. this.$Modal.warning({
  64. title: this.$L('上传失败'),
  65. content: this.$L('文件 % 上传失败,%', file.name, res.msg)
  66. });
  67. this.$emit('on-error', file);
  68. this.$refs.upload.fileList.pop();
  69. }
  70. },
  71. handleFormatError(file) {
  72. //上传类型错误
  73. this.$Modal.warning({
  74. title: this.$L('文件格式不正确'),
  75. content: this.$L('文件 % 格式不正确,仅支持上传:%', file.name, this.uploadFormat.join(','))
  76. });
  77. },
  78. handleMaxSize(file) {
  79. //上传大小错误
  80. this.$Modal.warning({
  81. title: this.$L('超出文件大小限制'),
  82. content: this.$L('文件 % 太大,不能超过%。', file.name, $A.bytesToSize(this.maxSize * 1024))
  83. });
  84. },
  85. handleBeforeUpload() {
  86. //上传前判断
  87. this.params = {
  88. username: this.target,
  89. token: $A.getToken(),
  90. };
  91. return true;
  92. },
  93. handleClick() {
  94. //手动上传
  95. if (this.handleBeforeUpload()) {
  96. this.$refs.upload.handleClick()
  97. }
  98. },
  99. upload(file) {
  100. //手动传file
  101. if (this.handleBeforeUpload()) {
  102. this.$refs.upload.upload(file);
  103. }
  104. },
  105. }
  106. }
  107. </script>