Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file object from file Input

Tags:

I'm trying to use a react-bootstrap file input with jquery fileupload(). With straight jquery I would do $('#fileUpload').prop('files') to get the files to pass to the fileupload call. However, I don't know how to get the files correctly with react-bootstrap.

<Input type='file' label='Upload' accept='.txt' ref='fileUpload' buttonAfter={uploadFileButton}/> 
like image 873
Joe W Avatar asked May 27 '15 13:05

Joe W


People also ask

How do you take input from a file?

In order to read information from a file, or to write information to a file, your program must take the following actions. 1) Create a variable to represent the file. 2) Open the file and store this "file" with the file variable. 3) Use the fprintf or fscanf functions to write/read from the file.

How do I get the value of a file input in react?

To hold file input's value in React, we can store the selected file in a state. We create the file state with the useState hook. Then we add the handleChange function that gets the selected file from the e.

How does file input work?

The input element, having the "file" value in its type attribute, represents a control to select a list of one or more files to be uploaded to the server. When the form is submitted, the selected files are uploaded to the server, along with their name and type.

How do you assign a value to a file input?

The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.


1 Answers

The ref string attribute is considered legacy, and will most likely be deprecated at some point in the future. The way to do this now is with a callback ref. Below I demonstrate using an ES6 arrow function.

Change your input element to:

<Input    type='file' label='Upload' accept='.txt'    buttonAfter={uploadFileButton}    ref={(ref) => this.fileUpload = ref} /> 

Then you can use:

const file = this.fileUpload.files[0];

And so on.

like image 189
fahad Avatar answered Oct 19 '22 09:10

fahad