vue 中formData方式上传文件

时间:2025-01-22 08:11:21
<el-upload
     action="#"
     v-model="fileList"
     accept=".xls, .xlsx"
     :show-file-list="false"
     :multiple="false"
     :on-change="onChange"
     :auto-upload="false">
     <el-button slot="trigger" size="small" type="primary" :loading="loadingBtn">上传</el-button>
</el-upload>
export default {
    data () {
        return {
            loadingBtn: false,
            fileList: [],
        }
    },
    methods: {
        // 附件选择
        onChange(file, fileList) {
            if ( > 0) {
                 = [fileList[ - 1]]//这一步,是 展示最后一次选择文件
                 = true;
                let formData = new FormData();
                ("file", [0].raw);
                (formData,'post').then(() => {
                    this.$('上传成功!')
                     = false;
                }).catch(() => {
                     = false;
                })
            }
        },
    },
}