如何为Ant设计Vue编写customRequest for Upload组件

pcrecxhr  于 2023-05-07  发布在  Vue.js
关注(0)|答案(2)|浏览(227)

我试过How should customRequest be set in the Ant Design Upload component to work with an XMLHttpRequest?,但它不适用于ant design vue。谁能写一个例子吗?

slwdgvem

slwdgvem1#

这是HTML组件:

<a-upload-dragger
    name="file"
    :multiple="true"
    :customRequest="uploadfiles"
    @change="handleChange">
</a-upload-dragger>

您需要处理customRequest:

uploadfiles({ onSuccess, onError, file }) {
    this.upload(file)
      .then(() => {
        onSuccess(null, file);
      })
      .catch(() => {
        console.log('error');
      });
  };

文件具有这样的结构:

name: "YourFileName.txt"
lastModified: ...
lastModifiedDate: ...
webkitRelativePath: ""
size: 24
type: "text/plain"
uid: "vc-upload-xxxxxx..."

您可以实现自己的上传功能。
您可以处理文件上传进度的状态变化:

handleChange(info) {
    const status = info.file.status;
    if (status !== 'uploading') {
      // show update progress console.log(info.file, info.fileList);
    }
    if (status === 'done') {
      // show success message
    } else if (status === 'error') {
      // show error message
    }
}
osh3o9ms

osh3o9ms2#

<a-upload
    accept=".zip"
    name="file"
    v-model:file-list="dict.upload_file_list"
    :customRequest="async () => { }"
    :beforeUpload="(file: File, filelist: any[]) => functions.handle_local_before_file_upload(file)"
  >
    <a-button>
      <upload-outlined></upload-outlined>
      Click to Upload a Backup File
    </a-button>
  </a-upload>

相关问题