Home >Web Front-end >Vue.js >A brief analysis of how to customize the upload method of AntdV Upload component customRequest

A brief analysis of how to customize the upload method of AntdV Upload component customRequest

青灯夜游
青灯夜游forward
2021-12-24 18:26:445200browse

How to customize the upload method of AntdV Upload component customRequest? The following article will introduce to you the custom upload method of Ant Design Vue's Upload component customRequest. I hope it will be helpful to you!

A brief analysis of how to customize the upload method of AntdV Upload component customRequest

customRequest You can customize your own upload method

Demand scenarios

Backend The management system and UI framework are Ant Design of Vue and the Upload component is used to upload images.

Requirement description: Upload images and convert them to base64

Implementation method

In the API method, there is a method to customize the upload behavior, by overwriting The default upload behavior, you can customize your own upload implementation

customRequestcustom upload method

<a-form-item
  :labelCol="labelCol"
  :wrapperCol="wrapperCol"
  label="照片">
  <a-upload
    v-decorator="[&#39;zp&#39;, validatorRules.zp]"
    listType="picture-card"
    class="avatar-uploader"
    :showUploadList="false"
    :beforeUpload="beforeUpload"
    :customRequest="selfUpload"
  >
    <img v-if="picUrl" :src="getAvatarView()" alt="头像"   style="max-width:90%"/>
    <div v-else>
      <a-icon :type="uploadLoading ? &#39;loading&#39; : &#39;plus&#39;" />
      <div class="ant-upload-text">上传</div>
    </div>
  </a-upload>

</a-form-item>

The uploaded image is converted to base64

//对上传的文件处理
selfUpload ({ action, file, onSuccess, onError, onProgress }) {
     console.log(file, &#39;action, file&#39;);
     const base64 = new Promise(resolve => {
         const fileReader = new FileReader();
         fileReader.readAsDataURL(file);
         fileReader.onload = () => {
              resolve(fileReader.result);
              // this.formImg = fileReader.result;
          }
      });
}

【Related recommendation: "vue.js Tutorial"】

The above is the detailed content of A brief analysis of how to customize the upload method of AntdV Upload component customRequest. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete