Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 获取Ant Design多上传列表的Base64字符串_Reactjs_Typescript_Antd - Fatal编程技术网

Reactjs 获取Ant Design多上传列表的Base64字符串

Reactjs 获取Ant Design多上传列表的Base64字符串,reactjs,typescript,antd,Reactjs,Typescript,Antd,嗨,我正在用Ant设计制作一个多上传文件按钮。我的目标是用文件列表更新状态fileList,并将originFileObj转换为base64字符串。问题是我的函数只为文件列表中的所有文件返回一个base64字符串。这是我的密码: import React from 'react'; import { Upload } from 'antd'; export default class MultiUpload extends React.Component { constructor(pro

嗨,我正在用Ant设计制作一个多上传文件按钮。我的目标是用文件列表更新状态fileList,并将originFileObj转换为base64字符串。问题是我的函数只为文件列表中的所有文件返回一个base64字符串。这是我的密码:

import React from 'react';
import { Upload } from 'antd';

export default class MultiUpload extends React.Component {
  constructor(props: any) {
    super(props);
    this.state = {
      fileList: []
    };
    this.handleUpload = this.handleUpload.bind(this);
  }

  handleUpload =  (info: any) => {
    let fileList = [...info.fileList];
    // Accept 5 files only
    fileList = fileList.slice(-5);
    fileList.forEach(function(file, index) {
      let reader = new FileReader();
      reader.onload = (e) => {
         file.base64 =  e.target.result;
      };
      reader.readAsDataURL(info.file.originFileObj);
    });
    this.setState({fileList: fileList});
  };

  componentDidUpdate(prevProps, prevState) {
    console.log(this.state)
  }

  render() {
    return (
      <div>
        <Upload
          listType={"text"}
          multiple={true}
          onChange={this.handleUpload}
        >
          <button >
            Upload
          </button>
        </Upload>
      </div>
    )
  }
};
从“React”导入React;
从“antd”导入{Upload};
导出默认类MultiUpload扩展React.Component{
构造器(道具:任何){
超级(道具);
此.state={
文件列表:[]
};
this.handleUpload=this.handleUpload.bind(this);
}
handleUpload=(信息:任意)=>{
让fileList=[…info.fileList];
//仅接受5个文件
fileList=fileList.slice(-5);
forEach(函数(文件、索引){
let reader=new FileReader();
reader.onload=(e)=>{
file.base64=e.target.result;
};
reader.readAsDataURL(info.file.originFileObj);
});
this.setState({fileList:fileList});
};
componentDidUpdate(prevProps、prevState){
console.log(this.state)
}
render(){
返回(
上传
)
}
};

onСhange函数是使用具有此类字段的对象调用的

{
   file: {/ * ... * /},
   fileList: [/ * ... * /],
   event: {/ * ... * /},
}
始终只将当前文件传递到readAsDataURL。相反,您需要从文件列表中传递一个文件。 handleUpload函数应该如下所示:

handleUpload = (info: any) => {
    let fileList = [...info.fileList];
    // Accept 5 files only
    fileList = fileList.slice(-5);
    fileList.forEach(function (file, index) {
      let reader = new FileReader();
      reader.onload = (e) => {
        file.base64 = e.target.result;
      };
      reader.readAsDataURL(file.originFileObj);
    });
    this.setState({ fileList });
  };