Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Issues with Image uploading in React bootstrap

I have written the below mentioned code to upload an image into the React component with the help of bootstrap and I have two issues in it,

Problem:1) I wanted to give validations to allow only .png/.jpg images but even if i am uploading .jpg/.png image, I am getting the alert that "File does not support. You must use .png or .jpg" which is not supposed to be shown.

Problem:2) I wanted the user to upload the image and i made the component according to that. However, i want to give one dummy image to be shown in the div before even user is uploading the image. So, how exactly can i do that?

Any help regarding the same is highly appreciated....!

Code:

import React, { Component, useState } from "react";
import "./style.css";

const FinalModeling = () => {
  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">
            <div className="modeling-text">
            </div>
          </div>
        </div>
        <img
          src={"file"}
          alt={""}
          width="180"
          height="220"
          text-align="left"
          style={{ display: "block" }}
          class="img-thumbnail"
        />
        
      </div>
      <div class="input-group mt-1 offset-1">
                <div class="custom-file smaller-input">
                  <input
                    type="file"
                    className="custom-file-input"
                    name="file"
                    inputProps={{ accept: "image/*" }}
                    onChange={fileChangedHandler}
                    id="inputGroupFile01"
                  />
                  <label class="custom-file-label" for="inputGroupFile01">
                    Choose your image
                  </label>
                </div>
              </div>
    </div>
  );
};

export default FinalModeling;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are checking the filetype with the wrong syntax. Instead of checking its type, you are trying to check the file itself.

1.) Do it like this:

if (file.type != ".png" || file.type != ".jpg")

2.) And for your second problem, You are not importing your image correctly.

-> First you have to import your image at the top.

import nameOfImage from './icon.png'

-> And then you can use it in the img tag.

<img src={nameOfImage} />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!