在vue-cli中使用iview组件上传多个文件的功能

在vue-cli中使用iview组件上传多个文件的功能

思路:创建一个数组 把需要上传的文件 push到这个数组里面
需求:

点击提交按钮把上传后得文件URL地址当做参数传给后台;多个文件使用逗号隔开

样式随便写的,自己做的demo
img

html代码

<template>
  <div>
    <Upload
      ref="upload"
      multiple  //支持多选文件
      name="files"  //上传接口的参数
      :before-upload="handleUpload"  //上传之前调用
      :on-success="uploadSuccess"  //上传成功调用
      :on-error="uploadError"  //上传失败调用
      :format="['docx','doc','txt', 'pdf']"  文件上传的格式
      :on-format-error="handleFormatError"   //文件格式错误调用
      action="http://192.10.78.68:8060/FH_AM6/api/fileUpload/upload"  //上传的接口地址
    >
      <Button icon="ios-cloud-upload-outline">上传</Button>
    </Upload>
    <Button type="primary" @click="issues" class="issues">提交</Button>
  </div>
</template>

js代码

<script>
    export default {
        name: "ces",
      data(){
          return{
            uploadList: [],
            attachments:"" //附件
          }
      },
      created() {

      },
      //监听attachments对象的值,有新值添加进一个数组
      watch: {
        attachments:function(val,oldval){
          if(val){
            this.uploadList.push(val)
            //   console.log(this.uploadList)
          }
        }
      },
      methods:{
        //上传成功调用
        uploadSuccess(response){
          this.attachments = response
        },
        //上传失败调用
        uploadError(error){
          this.$Message.info(error);
        },
        //文件格式错误调用
        handleFormatError(){
          this.$Message.info("文件格式不正确,请上传excel格式文件");
        },
        //点击提交传参
        issues() {
          if (this.uploadList.length == 1){
            this.attachments = this.uploadList[0].url
            console.log(this.attachments)
          }else if(this.uploadList.length > 1){
            const urlList = []
            for(let j = 0;j<this.uploadList.length;j++){
              urlList.push(this.uploadList[j].url)
              //  console.log(urlList)
            }

            this.attachments = urlList.join(",")
            //   console.log(this.attachments)
          }  
          else {
            this.$ajax.post('http://192.10.78.68:8060/FH_AM6/api/manuscript/saveManuscript',
              this.$qs.stringify({
                FILE_PATHS:this.attachments,  //附件
              })
            ).then((res)=>{
              if(res.status==200){
                this.$router.push('/contributefinish')
              }
            }).catch(()=>{
              this.$Message.error('获取失败');
            })
          }
        },
        }
      }


</script>

整体代码

<template>
  <div>
    <Upload
      ref="upload"
      multiple
      name="files"
      :before-upload="handleUpload"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :format="['docx','doc','txt', 'pdf']"
      :on-format-error="handleFormatError"
      action="http://192.10.78.68:8060/FH_AM6/api/fileUpload/upload"
    >
      <Button icon="ios-cloud-upload-outline">上传</Button>
    </Upload>
    <Button type="primary" @click="issues" class="issues">提交</Button>
  </div>
</template>

<script>
    export default {
        name: "ces",
      data(){
          return{
            uploadList: [],
            attachments:"" //附件
          }
      },
      created() {

      },
      //监听attachments对象的值,有新值添加进一个数组
      watch: {
        attachments:function(val,oldval){
          if(val){
            this.uploadList.push(val)
            //   console.log(this.uploadList)
          }
        }
      },
      methods:{
        //上传成功调用
        uploadSuccess(response){
          this.attachments = response
        },
        //上传失败调用
        uploadError(error){
          this.$Message.info(error);
        },
        //文件格式错误调用
        handleFormatError(){
          this.$Message.info("文件格式不正确,请上传excel格式文件");
        },
        //点击提交传参
        issues() {
          if (this.uploadList.length == 1){
            this.attachments = this.uploadList[0].url
            console.log(this.attachments)
          }else if(this.uploadList.length > 1){
            const urlList = []
            for(let j = 0;j<this.uploadList.length;j++){
              urlList.push(this.uploadList[j].url)
              //  console.log(urlList)
            }

            this.attachments = urlList.join(",")
            //   console.log(this.attachments)
          }
          else {
            this.$ajax.post('http://192.10.78.68:8060/FH_AM6/api/manuscript/saveManuscript',
              this.$qs.stringify({
                FILE_PATHS:this.attachments,  //附件
              })
            ).then((res)=>{
              if(res.status==200){
                this.$router.push('/contributefinish')
              }
            }).catch(()=>{
              this.$Message.error('获取失败');
            })
          }
        },
        }
      }


</script>

<style scoped>
.issues{
  float: right;
}
</style>

以上是本章的全部内容

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值