前端如何实现FormData多文件上传功能并且传入参数

这段代码展示了如何在前端使用Element UI组件实现多文件上传功能,通过`el-upload`组件监听`on-change`、`on-success`等事件来处理文件上传过程。同时,将上传文件和其他表单数据(如`describe`、`contact`、`phone`)一起发送到后台,通过`FormData`对象附加额外参数。在上传过程中,限制了最多上传4个文件,并在达到限制时禁用上传按钮。
摘要由CSDN通过智能技术生成

前端如何实现FormData多文件上传功能并且传入参数

            <el-form-item label="图片上传(选填)" prop="people">
                <div class="errorUpload">
                    <el-upload
                        action="#"
                        v-if="isphoneLength == true"
                        ref="upload"
                        list-type="picture-card"
                        :auto-upload="false"                     //多文件上传时改为false 就不用再每次上传文件时调用接口函数 
                        :file-list="fileList"                           //file文件列表
                        :on-success="handleUpload"      //上传成功的回调
                        :on-error="handleupError"          // 失败文件的回调
                        :on-progress="handlePro"
                        :on-remove="onRemove"
                        :limit="4"                                   // 限制上传最大上传数量
                        :on-change="handleChange"   //上传时触发文件改变功能
                        accept="bmp, jpg, png, gif, jpeg"
                        :class="{ disabled: uploadDisabled }"
                    >
                        <div style="marginTop:-37px"><i class="el-icon-plus"></i></div>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="" />
                    </el-dialog>
                    <div>
                        <span class="one"> {{ phoneLength }} </span><span class="two">/4</span>
                    </div>
                </div>
            </el-form-item>
            <el-form-item style="textAlign:center;marginTop:38px">
                <el-button style="width:200px;height:50px" type="primary" @click="submitForm('ruleForm')"
                    >提交</el-button
                >
            </el-form-item>
        </el-form>     
        <script>
 import { addErrorApi, upLoadApi } from '@/api/heat.js'
 export default {
data() {
    return {
        ruleForm: {
            name: '',
            phone: '',
            people: '',
        },
        rules: {
            name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        },
        isErrorCorrection: true,
        dialogImageUrl: '',
        dialogVisible: false,
        valueLength: 0,
        phoneLength: 0,
        isphoneLength: true,
        accountId: '',
        UaSysErrorCor: {
            accountId: '',
        },
        // dialogImageUrl: '',
        // dialogVisible: false,
        // uploadUrls: this.BASE_URL + '/admin/upload_image',
        fileList_now: [],
        fileList: [],
        id: '',
        uploadDisabled: false,
    }
},
watch: {
    'ruleForm.name'(val) {
        if (val) {
            this.valueLength = val.length
        } else {
            this.valueLength = 0
        }
    },
    fileList_now(val) {
        if (val) {
            this.phoneLength = val.length
            if (this.phoneLength == 4) {
                this.uploadDisabled = true
                console.log('this.isphoneLength', this.isphoneLength)
            } else {
                this.uploadDisabled = false
            }
        } else {
            this.phoneLength = 0
        }
    },
},
methods: {
    submitForm() {
        this.$refs.ruleForm.validate(valid => {
            let param = this.ruleForm
            this.UaSysErrorCor.describe = param.name
            this.UaSysErrorCor.contact = param.people
            this.UaSysErrorCor.phone = param.phone
            console.log('this.UaSysErrorCor', this.UaSysErrorCor)
            if (valid) {
                addErrorApi(this.UaSysErrorCor).then(res => {
                    if (res.data) {
                        this.id = res.data
                        console.log('this.id9999', this.id)
                        this.UaSysErrorCor.describe = ''
                        this.UaSysErrorCor.contact = ''
                        this.UaSysErrorCor.phone = ''
                        // this.sendCancle()
                        this.upLoadImg(res.data)
                    }
                })
                console.log('id222', this.id)
            } else {
                return false
            }
        })
    },
    // 上传图片接口
    upLoadImg(id) {
        console.log('this.id3', id)
        const fd = new FormData()
        if (this.fileList_now) {
            this.fileList_now.forEach(file => {
                console.log('file', file)
                console.log('file', file.raw)
                fd.append('picture', file.raw)
            })
            fd.append('id', id)    //可以额外添加参数
            console.log('this.id', this.id)
            console.log('fd', fd)
            upLoadApi(fd).then(res => {
                if (res) {
                    this.sendCancle()
                }
            })
        } else {
            this.sendCancle()
        }
    },
    handleRemove(file) {
        console.log(file)
    },
    //获取图片
    getUpload(file) {
        console.log('file', file)
    },
    handlePictureCardPreview(file) {
        console.log('file', file.url)
        this.dialogImageUrl = file.url
        this.dialogVisible = true
    },
    handleDownload(file) {
        console.log(file)
    },
    // 上传成功
    handleUpload(response, file) {
        console.log('response', response)
        console.log('file3', file)
    },
    //上传失败
    handleupError(err, file) {
        console.log('err', err)
        console.log('file2', file)
    },
    // 上传时
    handlePro(event, file) {
        console.log('event', event)
        console.log('file1', file)
    },
    // 移除时
    onRemove(file, fileList) {
        // this.uploadDisabled = false
        console.log('fileList', fileList)
        console.log('file1', file)
    },
    // 提交时
    handleChange(file, fileList) {
        this.fileList_now = fileList
        console.log('this.fileList_now', this.fileList_now)
    },
}
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值