Upload.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: 10240
  28. }
  29. },
  30. data() {
  31. return {
  32. uploadFormat: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt'],
  33. actionUrl: $A.apiUrl('chat/files/upload'),
  34. params: {
  35. username: this.target,
  36. token: $A.getToken(),
  37. }
  38. }
  39. },
  40. mounted() {
  41. },
  42. watch: {},
  43. methods: {
  44. handleProgress(event, file) {
  45. //上传时
  46. if (typeof file.tempId === "undefined") {
  47. file.tempId = $A.randomString(8);
  48. this.$emit('on-progress', file);
  49. }
  50. },
  51. handleSuccess(res, file) {
  52. //上传完成
  53. if (res.ret === 1) {
  54. for (let key in res.data) {
  55. if (res.data.hasOwnProperty(key)) {
  56. file[key] = res.data[key];
  57. }
  58. }
  59. this.$emit('on-success', file);
  60. } else {
  61. this.$Modal.warning({
  62. title: this.$L('上传失败'),
  63. content: this.$L('文件 % 上传失败,%', file.name, res.msg)
  64. });
  65. this.$emit('on-error', file);
  66. this.$refs.upload.fileList.pop();
  67. }
  68. },
  69. handleFormatError(file) {
  70. //上传类型错误
  71. this.$Modal.warning({
  72. title: this.$L('文件格式不正确'),
  73. content: this.$L('文件 % 格式不正确,仅支持上传:%', file.name, this.uploadFormat.join(','))
  74. });
  75. },
  76. handleMaxSize(file) {
  77. //上传大小错误
  78. this.$Modal.warning({
  79. title: this.$L('超出文件大小限制'),
  80. content: this.$L('文件 % 太大,不能超过%。', file.name, this.maxSize + 'KB')
  81. });
  82. },
  83. handleBeforeUpload() {
  84. //上传前判断
  85. this.params = {
  86. username: this.target,
  87. token: $A.getToken(),
  88. };
  89. return true;
  90. },
  91. handleClick() {
  92. //手动上传
  93. if (this.handleBeforeUpload()) {
  94. this.$refs.upload.handleClick()
  95. }
  96. },
  97. upload(file) {
  98. //手动传file
  99. this.$refs.upload.upload(file);
  100. },
  101. }
  102. }
  103. </script>