0

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

在beforeUpload(file, fileList) {
    console.log(file, fileList)
        self.setState({
          file
        })
    return false
},
zhushao 243
11月9日提问

1 个回答

0

使用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("文件上传失败!");
            },
          });
}

推广链接