Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda