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

169
Views
How to create an input formfield with button that uploads a file using react and javascript?

i am new to programming and learning on the go. what i am trying to do currently is to create a form field like below that uploads a file on clicking a button.

enter image description here

and once file uploaded and before submitting the file to api should see the file name in form field like below

enter image description here

i have tried like below but that shows only browse file button but doesnt sow the input field with Add file placeholder.

<input
    type="file"
    id={`${fieldRoot}.file`}
    ref={fileInput}
    style={{ display: 'none' }}
    accept=".pem"
    onChange={() => {
        /*do something*/
            console.log('hello')
     }}
 />
 <Button
     minWidth="126px"
     //onClick={handleUploadClick}
 >
     Browse file
 </Button>

Could someone help me with this. thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • <input type="file"/> also is a input. So you can use <label> tag and style it look like a button. In this case, I use image instead.

  • I'm using hidden attribute to make the actual <input /> invisible.

          <input
            type="file"
            id="avatar"
            onChange={fileSelectedHandler}
            hidden
          />
          <label className={profileStyle.uploadLabel} htmlFor="avatar">
            <Image
              width="25px"
              height="25px"
              src="/icons/upload-image-icon.jpeg"
              alt="upload-avatar"
            />
          </label>

Here is what you want:

import React, { useState } from "react";

export const Upload = () => {
  const [filename, setFilename] = useState("");
  return (
    <>
      <input
        value={filename}
        type="text"
        style={{
          width: 200,
          height: 28,
          padding: 0,
          border: "1px solid #767676"
        }}
      />
      <input
        type="file"
        id="upload"
        hidden
        onChange={(e) => {
          // You have your file detail in e.tartget.files
          console.log(e.target.files[0]);
          setFilename(e.target.files[0].name);
        }}
      />
      <label
        htmlFor="upload"
        style={{
          width: 90,
          background: "#767676",
          color: "#fff",
          padding: 5
        }}
      >
        Browse
      </label>
    </>
  );
};

Check this Demo

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!