I have a "choose file" image as the picture below. What I want to set the limit size and the file type for image upload, .png or .jpg only
My code is below. It looks like when the user chooses .png or .jpg or any file types, it pops up an alert message, and then the image will be approved. I want .png and .jpg works well, but other files chosen displayed an alert message
import React, {Component, useState} from 'react';
import axios from 'axios';
const Modeling = () => {
const [file, setFile] = useState(null);
const fileChangedHandler = event => {
let file = event.target.files[0];
let reader = new FileReader();
console.log(file);
reader.onload = function(e) {
setFile(e.target.result);
};
reader.readAsDataURL(event.target.files[0]);
if (file != ".png" || file !=".jpg" ) {
window.alert("File does not support. You must use .png or .jpg ");
return false;
}
if (file.size > 10e6) {
window.alert("Please upload a file smaller than 10 MB");
return false;
}
};
return (
<div id="modeling">
<div className="container">
<div className="row">
<div className="col-xs-12 col-md-8">
<div className="modeling-text">
<h2>3D MODELING</h2>
<h3>Upload a 2D image to get a 3D model</h3>
<input className="btn btn-secondary"
id="fileInput"
name="file" type="file"
inputProps={{ accept: 'image/*' }}
onChange={fileChangedHandler}
/>
<button className="btn btn-primary" style={{float:"left", marginLeft: "10px", marginBottom: "10px"}}
id="renderButton">
Render 3D Model
</button>
</div>
</div>
</div>
<img src={file} alt={""} width="400" height="400" text-align="left" style={{display:'block'}} />
</div>
</div>
)
}
export default Modeling;
Can anyone stop by for help? I am really appreciated for that