• Ant Design Vue的上传图片组件Upload封装和遇到的问题


    问题:上传图片后图片会打印两次的问题(因为什么引起的--也是看了文档找了一圈 主要是因为没有传uid这个参数 )

    1. const handleUploadSuccess = (file) => {
    2. let list = cloneDeep(props.fileList);
    3. const imgData = { ...file, url: file.address, name: file.originalFileName,
    4. uid: generateUid() }; // 这边绑定uid就好了
    5. list.push(imgData);
    6. updateFileList(list);
    7. };

    需求--封装Upload组件(这是我封装的 大家看看就好)

    1. <script setup lang="ts">
    2. import { uploadFileApi } from '@/api/corrosionInvestigate/index';
    3. import { getToken } from '@/utils/auth/index';
    4. import { PlusOutlined } from '@ant-design/icons-vue';
    5. import { message, Modal, Upload } from 'ant-design-vue';
    6. import { cloneDeep } from 'lodash-es';
    7. import { ref } from 'vue';
    8. const props = defineProps({
    9. limit: {
    10. type: Number,
    11. default: 9,
    12. },
    13. multiple: {
    14. type: Boolean,
    15. default: true, // 多选
    16. },
    17. disabled: {
    18. type: Boolean,
    19. default: false,
    20. },
    21. showUploadList: {
    22. type: Boolean,
    23. default: true,
    24. },
    25. fileList: {
    26. type: Array,
    27. default: () => [],
    28. },
    29. });
    30. const emits = defineEmits(['update:fileList']);
    31. const accept = '.jpeg,.jpg,.png';
    32. const headers = { Authorization: getToken() };
    33. const customRequest = async (uploadFile) => {
    34. let formData = new FormData();
    35. formData.append('file', uploadFile.file);
    36. try {
    37. const res = await uploadFileApi(formData);
    38. if (res) {
    39. const file = res.data;
    40. // console.log(file, '成功地址');
    41. handleUploadSuccess(file);
    42. } else {
    43. message.error(`返回文件信息错误,联系管理员!`);
    44. }
    45. } catch (error) {
    46. console.log(error);
    47. }
    48. };
    49. const updateFileList = (array) => {
    50. emits('update:fileList', array);
    51. };
    52. const generateUid = () => {
    53. const uuid = Math.random().toString(24).substring(2, 12);
    54. return uuid;
    55. };
    56. const handleUploadSuccess = (file) => {
    57. let list = cloneDeep(props.fileList);
    58. const imgData = { ...file, url: file.address, name: file.originalFileName, uid: generateUid() };
    59. list.push(imgData);
    60. updateFileList(list);
    61. };
    62. const filterFileList = (file) => {
    63. let list = cloneDeep(props.fileList);
    64. list = list.filter((i: any) => i.uid !== file.uid);
    65. updateFileList(list);
    66. };
    67. const handleUploadError = (file) => {
    68. message.error(`${file.name} 上传失败`);
    69. filterFileList(file);
    70. };
    71. const handleRemove = (file) => {
    72. filterFileList(file);
    73. };
    74. const handleUploadChange = ({ file, fileList: list }) => {
    75. switch (file.status) {
    76. case 'removed':
    77. handleRemove(file);
    78. break;
    79. case 'done':
    80. handleUploadSuccess(file);
    81. break;
    82. case 'error':
    83. handleUploadError(file);
    84. break;
    85. default:
    86. console.log(file, 'change');
    87. }
    88. /*
    89. // 2. read from response and show file link
    90. resFileList = resFileList.map(file => {
    91. if (file.response) {
    92. // Component will show file.url as link
    93. file.url = file.response.url;
    94. }
    95. return file;
    96. });
    97. */
    98. };
    99. function getBase64(file) {
    100. return new Promise((resolve, reject) => {
    101. const reader = new FileReader();
    102. reader.readAsDataURL(file);
    103. reader.onload = () => resolve(reader.result);
    104. reader.onerror = (error) => reject(error);
    105. });
    106. }
    107. const previewVisible = ref(false);
    108. const previewImage = ref('');
    109. const previewTitle = ref('');
    110. const handleCancel = () => {
    111. previewVisible.value = false;
    112. previewTitle.value = '';
    113. };
    114. const handlePreview = async (file) => {
    115. if (!file.url && !file.preview) {
    116. file.preview = await getBase64(file.originFileObj);
    117. }
    118. previewImage.value = file.url || file.preview;
    119. previewVisible.value = true;
    120. previewTitle.value = file.name || file.url.substring(file.url.lastIndexOf('/') + 1);
    121. };
    122. script>
    123. <style scoped lang="scss">
    124. /* you can make up upload button and sample style by using stylesheets */
    125. .ant-upload-select-picture-card i {
    126. color: #999;
    127. font-size: 32px;
    128. }
    129. .ant-upload-select-picture-card .ant-upload-text {
    130. margin-top: 8px;
    131. color: #666;
    132. }
    133. style>

  • 相关阅读:
    java中PriorityQueue队列的使用
    Kaggle 专利匹配比赛赛后总结
    C# 核心8——多态
    优炫软件董事长梁继良当选新一届北京市商会副会长
    Transform介绍 - 源码分析(3)
    《OpenHarmony开源鸿蒙学习入门》–API9的Stage模型说明
    力扣学习笔记——49. 字母异位词分组
    面试算法问题
    mysql原理
    Java基础-day03
  • 原文地址:https://blog.csdn.net/2301_76607997/article/details/138161582