JS导入文件并调用接口提交(FormData格式)

<ul>
    <li>
        <span>文件:</span>
        <input type="text" v-model="nameVal">
        <div style="float:right;marginTop:6px">
            <label for="fileInput">
                <i aria-hidden="true" class="cursor" :disabled="progressFlag">选择文件</i>
            </label>
            <input
                 v-show="false"
                 type="file"
                 id="fileInput"
                 @change="handleFileChange"
                 name="file"
                 ref="file"
            />
        </div>
    </li>
    <li class="uploadBox" v-if="isShow">
        <h3>文件正在上传中……</h3>
    </li>
</ul>

<button @click="submitTolead">提交</button>
data(){
    return{
        progressFlag:false,
        isShow:false,
        nameVal:"",//文件名
        updateFile:""//文件
    }
}
methords:{
    // 选择文件
    handleFileChange(e) {
        let _this = this;
        let file = e.target.files;
         // console.log(file,"单文件流文件流");
        _this.updateFile = file[0];
        // console.log(_this.updateFile);
        // console.log(file[0].name);
        let val = file[0].name;
        this.nameVal = val;
        e.target.value = '';
    },
}

调用后端接口,上传文件,格式:FormData

// 导入提交
submitTolead(){
    this.progressFlag = true;
    this.isShow = true;
    let formData = new FormData();
    formData.append("appID",this.appID)
    formData.append("pageCode",this.pageCode)
    formData.append("userToken",this.token)
    formData.append("userAccount",this.uAccount)
    formData.append("userName",this.userName)
    formData.append("itemID",this.rowValue.ITEMID)
    formData.append("file",this.updateFile)
    this.$axios.post("/api2/StockIn/ImportBarOfWWRK",formData).then(res=>{
        // console.log(res);
        if(res.data.Code == 200){
            this.progressFlag = false;
            this.isShow = false;
            this.$message({
                message: '导入成功!',
                type: 'success'
            });
            // 关闭导入弹窗
            this.closeToLeadDig()
        }else{
            this.progressFlag = false;
            this.isShow = false;
            alert(res.data.Msg)
        }
    })
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值