在element  ui中上传文件时,使用FormData来上传

如下

<el-upload
                    id="importFile"
                    class="upload-demo"
                    :limit="1"
                    ref="uploads"
                    :file-list="fileList"
                    :http-request="uploadFile"
                    :before-upload="beforeAvatarUpload"
                    :on-change="imageChange                              
                    :auto-upload="false"
                    :action="'xxxx/api/DataManagement/xxx'"
                    :data="uploadData"
                    
                    :on-success="uploadmodellist"
                  >





//method  中的方法


      uploadFile(param){
         var fileObj = param.file;
        var form = new FormData();   //在创建FormData对象时指定表单的元素  我们需要传的参数

        form.append("file", param.file);
       form.append("monitorUnitReportId", this.MonitorUnitReportId);
       axios({
         method: 'POST',
         url: 'http://xxxxx/api/DataManagement/xxxx',
         headers: {
           'Content-Type': 'multipart/form-data'
         },
         data:form
       }).then(res => {
         res
         debugger
         if(res.data.msgCode == '1'){
           this.$message({
             type:'success',
             message:res.data.msgDesc
           })
           this.attachMentCode = res.data.data.attachmentCode//附件编码
           this.attachmentId = res.data.data.attachmentId//标志
         } 
       })
     },
     imageChange(file){
       this.uploadname=file.name.substring(0,file.name.indexOf("."))
      
     },
    beforeAvatarUpload(file) {
   

      
        const isM= file.type === 'application/msword';
        const isLt50M = file.size / 1024 / 1024 < 50;
        

        if (!isM) {
          this.$message.error('上传的附件只能是 doc 格式!');
        }
        if (!isLt50M) {
          this.$message.error('上传的附件图片大小不能超过 50MB!');
        }
        return isM && isLt50M;

         
      },

 

Logo

前往低代码交流专区

更多推荐