ant-file预览

该代码示例展示了如何在Vue应用中利用AntDesignVue组件进行文件上传,设置图片预览功能。通过``组件,配置了上传列表类型、服务器端接口地址以及自定义的预览文件逻辑,预览文件功能通过fetchAPI模拟从远程获取缩略图。
摘要由CSDN通过智能技术生成
<template>
  <div>
    <a-upload
      v-model:file-list="fileList"
      list-type="picture"
      action="//jsonplaceholder.typicode.com/posts/"
      :preview-file="previewFile"
    >
      <a-button>
        <upload-outlined></upload-outlined>
        Upload
      </a-button>
    </a-upload>
  </div>
</template>
<script lang="ts">
import { UploadOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref } from 'vue';
import type { UploadProps } from 'ant-design-vue';

export default defineComponent({
  components: {
    UploadOutlined,
  },
  setup() {
    const previewFile: UploadProps['previewFile'] = async file => {
      console.log('Your upload file:', file);
      // Your process logic. Here we just mock to the same file
      const res = await fetch('https://next.json-generator.com/api/json/get/4ytyBoLK8', {
        method: 'POST',
        body: file,
      });
      const { thumbnail } = await res.json();
      return thumbnail;
    };
    return {
      previewFile,
      fileList: ref([]),
    };
  },
});
</script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于 antd-pro 版本 v5.2.0 没有提供上传文件/图片组件,因此需要自己编写。以下是一个简单的上传文件/图片组件,使用 antd-pro 的 Upload 和 Modal 组件。 ```javascript import React, { useState } from 'react'; import { Upload, Modal } from 'antd'; import { PlusOutlined } from '@ant-design/icons'; const UploadImage = ({ value, onChange, maxCount = 1, accept = 'image/*' }) => { const [previewVisible, setPreviewVisible] = useState(false); const [previewImage, setPreviewImage] = useState(''); const handleCancel = () => setPreviewVisible(false); const handlePreview = (file) => { setPreviewImage(file.url || file.thumbUrl); setPreviewVisible(true); }; const handleChange = ({ fileList }) => { onChange(fileList); }; const uploadButton = ( <div> <PlusOutlined /> <div style={{ marginTop: 8 }}>上传</div> </div> ); const fileList = value || []; return ( <> <Upload accept={accept} listType="picture-card" fileList={fileList} onPreview={handlePreview} onChange={handleChange} beforeUpload={(file) => { if (fileList.length >= maxCount) { message.warning(`最多只能上传 ${maxCount} 张图片`); return false; } }} > {fileList.length >= maxCount ? null : uploadButton} </Upload> <Modal visible={previewVisible} footer={null} onCancel={handleCancel}> <img alt="预览" style={{ width: '100%' }} src={previewImage} /> </Modal> </> ); }; export default UploadImage; ``` 使用示例: ```javascript import React, { useState } from 'react'; import UploadImage from './UploadImage'; const Demo = () => { const [images, setImages] = useState([]); const handleChange = (fileList) => { setImages(fileList); }; return ( <div> <UploadImage value={images} onChange={handleChange} maxCount={3} accept="image/*" /> </div> ); }; export default Demo; ``` 在上面的示例中,使用 UploadImage 组件来上传图片,通过 value 和 onChange 属性来控制上传的图片。maxCount 属性用于限制最多上传的图片数量,accept 属性用于限制上传的文件类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值