Element+Axios上传图片 OR 文件

Vue template

<el-upload
    class="avatar-uploader"
    action
    name="file"
    :show-file-list="false"
    :http-request="upLoad" 
    > 
        <div class="upload-btn">上传</div> 
</el-upload>

Vue Methods

upLoad(fileObj) {
      uploadImg(fileObj)  // 这里是Axios请求方法
    },  

Axios请求

import request from '@/utils/request' // 实例化 Axios

export function uploadImg(fileObj,modular="") {
    let formData = new FormData();
    formData.set("file", fileObj.file);
    formData.set("modular", modular);
    return request({
      url: '/common/uploadImage',
      headers: {
        "Content-type": "multipart/form-data"
      },
      method: 'post',
      data: formData
    })
}
posted @ 2020-02-15 14:08  Hekiss  阅读(445)  评论(0编辑  收藏  举报