Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the size of an uploaded file React JS

How would I go about finding the size of an uploaded file in React JS?

like image 292
user3708761 Avatar asked Dec 10 '22 21:12

user3708761


2 Answers

Let's say, your jsx is like <input type="file" onChange={this.fileChangedHandler}/>

Now, in your fileChangedHandler, you'll have to do something like below:

fileChangedHandler = (event) => {
    let file_size = event.target.files[0].size;

    //or if you like to have name and type
    let file_name = event.target.files[0].name;
    let file_type = event.target.files[0].type;
   //do whatever operation you want to do here
};
like image 97
Plabon Dutta Avatar answered Dec 24 '22 16:12

Plabon Dutta


You will first need to grab the input type file upload like var uploadDocuments = document.getElementById('file-0'); //grab html file-upload element

and can get the file size using .size attribute.

uploadDocuments.files[0].size //this gives the size of uploaded file

like image 41
a.developer Avatar answered Dec 24 '22 16:12

a.developer