elementui的文件上传功能-.上传文件-带参数-手动

elementui的文件上传功能-.上传文件-带参数-手动

<el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" size="mini" style="padding-right:10px;padding-left:10px;padding-top:20px;">
<el-form-item label="参数:" prop="bacth">
                    <el-input v-model="formInline.bacth" placeholder="内容"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-upload
                        class="upload-demo upload-flex"
                        ref="upload"
                        action="#"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :http-request="httpRequest"
                        :file-list="fileList"
                        :on-success="handSuccess"
                        :on-change="handleChange"
                        :on-exceed='handleExceed'
                        :before-remove='beforeRemove'
                        :data="getUploadList"
                        :auto-upload="false"
                        accept=".txt"
                        :limit="1">
                        <el-button slot="trigger" size="mini" type="primary">选择文件</el-button>
                        <el-button style="margin-left: 10px;" size="mini" type="success" @click="submitUpload">上传新数据</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传txt文件</div>
                    </el-upload>
                </el-form-item>
            </el-form>


<script>
export default {
	data() {
        return {
	getNums:''
}
	methods: {
	getNum(value){
            this.getNums = value
        },
submitUpload(data) {//点击上传
            this.$refs.upload.submit(data);
        },
        handleChange(file, fileList) {//上传文件变化时
            this.fileList = fileList
        },
        async httpRequest(param){
            let file = param.file // 相当于input里取得的files
            let batch_name = this.formInline.bacth
            // console.log(file)
            let formData = new FormData()// FormData 对象
            formData.append('file', file)// 文件对象
            formData.append('batch_name', batch_name)
            let that = this;
            that.$axios({
                method: 'POST',
                url: url, 
                headers:{'Content-Type': 'multipart/form-data'},
                data: formData
            }).then((response) => {
                // console.log(response)
                if(response.data.ret) {
                    this.getBatchList();
                    this.$message.success('上传成功')
                    this.chuliShow = false
                }else{
                    this.$message.error(response.data.msg+'失败')
                    this.chuliShow = false
                }
            })
            .catch((e) => {
                this.$message.error('上传失败')
                this.chuliShow = false
            })
            
        },
        handSuccess(response, file, fileList){//上传成功提示并且清除列表
            this.chuliShow = true
            this.$refs.upload.clearFiles();
            this.formInline.bacth=''
            // console.log(response, file, fileList);
        },
        handleRemove(file, fileList) {//文件移除的
            // console.log(file, fileList);
        },
        handlePreview(file) {//点击上传文件的时候的
            // console.log(file);
        },
        handleExceed(files, fileList) {//文件限制超个数
            this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
        },
        beforeRemove(file, fileList) {//删除
            return this.$confirm(`确定移除 ${ file.name }`);
        },
}
}
</script>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一介青烟小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值