0

I'm using:

  <ReactFileReader handleFiles={this._getFiles}>
     <button className='btn'>Upload</button>
   </ReactFileReader>

On an spfx web part. It's a form that submits to an SPO list but I want a file to be attached to the list item. I've tried lots of other solutions but my knowledge is lacking on how to implement them. Finding the wrapper has made things potentially easier but I'm still clueless on how to set the upload to state. What type should the state item be initialised as? [], "", or null

Is there anything else I should know? Here is my function for the file upload:

private _getFiles = files => {
  const content = files.result;
  console.log('file content:', content);

  this.setState({
    FileUpload: files
  }, () => {
    console.log(this.state.FileUpload);
  });
  console.log(files);

}

In the console I'm getting [object FileLest]: 0: File, length: 1} but it reads out the title of the document that I've 'attached'. I've read this but I need it distilled into a basic code example: How to set file object in state with React hooks?

It's not setting anything to state though.

I suppose this is a start?

NightTom
  • 418
  • 15
  • 37

1 Answers1

1

You can use ref to fetch the file. Declare a variable in state. You can do this by defining the state in constructor of your component.

constructor(props) {
    super(props);
    this.state = {
      fileInput : React.createRef();
    }
}

Then use it as:

  <ReactFileReader handleFiles={this._getFiles} ref={this.state.fileInput}>
     <button className='btn'>Upload</button>
   </ReactFileReader>

Finally, in your _getFiles method:

console.log('Selected file - ', this.state.fileInput[0].name);

Hope this works for you.

Muhammad Zeeshan
  • 4,608
  • 4
  • 15
  • 41