综合办公系统
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

fileUpload.vue 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!--
  2. * @Author: ysh
  3. * @Date: 2025-03-18 11:14:52
  4. * @LastEditors: Please set LastEditors
  5. * @LastEditTime: 2025-03-19 14:05:47
  6. -->
  7. <template>
  8. <view>
  9. <view class="btn">
  10. <uv-button @click="chooseFile" text="+ 选择文件" type="primary" :disabled="!showBtn"></uv-button>
  11. </view>
  12. <view v-if="fileName" class="file-box">
  13. <view class="link">
  14. <uv-link :href="`${fileUrl + fileName}`" :text="docName" color="#19be6b" line-color="#19be6b"></uv-link>
  15. </view>
  16. <view class="delete" v-if="showBtn">
  17. <uv-icon name="trash" color="#f56c6c" size="18px" @click="deleteFile()"></uv-icon>
  18. </view>
  19. </view>
  20. <text v-if="showBtn">只能上传1个文件,重复上传将覆盖原文件</text>
  21. </view>
  22. </template>
  23. <script>
  24. import config from '@/config';
  25. import { getToken } from '@/utils/auth'
  26. import { getFileName } from '@/utils/common';
  27. import upload from '@/utils/upload'
  28. export default {
  29. props: {
  30. fileType: String,
  31. fileName: String,
  32. showBtn: Boolean
  33. },
  34. data() {
  35. return {
  36. value: [],
  37. tempFilePath: '',
  38. fileUrl: config.baseUrl + '/profile/upload',
  39. docName: ''
  40. }
  41. },
  42. watch: {
  43. fileName(newval) {
  44. this.docName = getFileName(newval);
  45. }
  46. },
  47. created() {
  48. },
  49. methods: {
  50. chooseFile() {
  51. uni.chooseFile({
  52. count: 1,
  53. type: 'all',
  54. success: (res) => {
  55. this.tempFilePath = res.tempFiles[0].path;
  56. let params = {
  57. url: '/common/upload',
  58. filePath: this.tempFilePath,
  59. name: 'file',
  60. formData: { // 附加表单数据
  61. fileType: this.fileType
  62. },
  63. }
  64. //上传文件
  65. upload(params).then(res => {
  66. if (res.code == 200) {
  67. let fileName = '/' + this.fileType + '/' + res.newFileName
  68. this.$modal.msgSuccess(res.msg);
  69. this.$emit('success', fileName)
  70. }
  71. })
  72. },
  73. fail: (err) => {
  74. console.error('选择文件失败:', err);
  75. }
  76. });
  77. },
  78. deleteFile() {
  79. uni.showModal({
  80. title: '提示',
  81. content: '是否删除文件?',
  82. success: (res) => {
  83. if (!res.confirm) {
  84. return;
  85. }
  86. // 继续执行后续代码
  87. this.$emit('deleteFile')
  88. }
  89. });
  90. }
  91. },
  92. }
  93. </script>
  94. <style lang="scss" scoped>
  95. .file-box {
  96. display: flex;
  97. height: 60px;
  98. align-items: center;
  99. border: 1px solid #ececec;
  100. padding: 10px;
  101. .link {
  102. flex: 1;
  103. }
  104. }
  105. .btn {
  106. width: 100px;
  107. height: 40px;
  108. }
  109. </style>