antd design上传文件时,怎样再提交表单时再进行上传?

想在beforeUpload中把文件对象存下来,但是好像不行

在beforeUpload(file, fileList) {
    console.log(file, fileList)
        self.setState({
          file
        })
    return false
},
阅读 793
评论
    1 个回答
    • 3.2k

    使用customRequest

    
    <Upload
          accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
          customRequest={this.customRequest}
          beforeUpload = {this.beforeUpload}
          fileList={this.state.fileList}
                    >
         <Button>
           <Icon type='upload' />上传
         </Button>
    </Upload>
    
    
    customRequest = (option)=> {
              const formData = new FormData();
              const fileUrl = AjaxUrl+"data/fileUpload.svt";
              formData.append('files[]',option.file);
             
              reqwest({ /*官网解释:It's AJAX
                            All over again. Includes support
                            for xmlHttpRequest, JSONP, CORS,
                             and CommonJS Promises A.*/
                url: fileUrl,
                method: 'post',
                processData: false,
                data: formData,
                success: (res) => {
                  //res为文件上传成功之后返回的信息,res.responseText为接口返回的值
                  let fileInfo = JSON.parse(res.responseText);
                  if(res){
                    this.setState({
                        fileInfo:fileInfo,
                        loading: false,
                        uploading: false,
                        defaultFile:false
                    })
                  }
                  
                },
                error: () => {
                  this.setState({
                      loading: false,
                      uploading: false
                  })
                  message.error("文件上传失败!");
                },
              });
    }