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

青灯夜游
Release: 2021-12-24 18:27:13
forward
5230 people have browsed it

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>
Copy after login

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;
          }
      });
}
Copy after login

【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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template