Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

image upload in react js

import { useState } from 'react';

function App() {
  const [imgfile, uploadimg] = useState([])
  	console.log("Image FIles",imgfile);
  const imgFilehandler = (e) => {
    if (e.target.files.length !== 0) {
      uploadimg(imgfile => [...imgfile, URL.createObjectURL(e.target.files[0])])
    }
  }
  return (
    <div className="App">
      <div>
        <center>
          <h2>Upload</h2>
          <input type="file" onChange={imgFilehandler} />
          <hr />
          <h2>Preview</h2>
          {imgfile.map((elem) => {
            return <>
              <span key={elem}>
                <img src={elem} height="200" width="200" alt="med1" />
              </span>
            </>
          })}
        </center>
      </div>
    </div>
  );
}
export default App;
Comment

upload image react

import React, { useState } from "react";

const UploadAndDisplayImage = () => {
  const [selectedImage, setSelectedImage] = useState(null);

  return (
    <div>
      <h1>Upload and Display Image usign React Hook's</h1>
      {selectedImage && (
        <div>
        <img alt="not fount" width={"250px"} src={URL.createObjectURL(selectedImage)} />
        <br />
        <button onClick={()=>setSelectedImage(null)}>Remove</button>
        </div>
      )}
      <br />
     
      <br /> 
      <input
        type="file"
        name="myImage"
        onChange={(event) => {
          console.log(event.target.files[0]);
          setSelectedImage(event.target.files[0]);
        }}
      />
    </div>
  );
};

export default UploadAndDisplayImage;
Comment

how to upload image in react js

import FileBase64 from "react-file-base64";
// FileBase64 <- use as component

<FileBase64 
	type="file"
    multiple={false} <- if want to upload multiple images set "true"
    onDone={} <- take callback function
/>

// onDone return an object of: filename, fileType, base64 data
// use the setState or function of useState to grap the base64 data
Comment

upload image with react

// reactjs codeto browse and upload an image from your device  
const addImage =async() => {

    const { value: file } = await Swal.fire({
      title: 'Select image',
      input: 'file',
      inputAttributes: {
        'accept': 'image/*',
        'aria-label': 'Upload your profile picture'
      }
    })
    
    if (file) {
      const reader = new FileReader()
      reader.onload = (e) => {
        Swal.fire({
          title: 'Your uploaded picture',
          imageUrl: e.target.result,
          imageAlt: 'The uploaded picture'
        }
        
        )
        console.log(e.target.result);
      }
     
      reader.readAsDataURL(file)
    }
    
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to iterate over keys in object javascript 
Javascript :: how to remove first child in javascript 
Javascript :: javascript generate random numbers 
Javascript :: find in string javascript 
Javascript :: disable button in swal popup 
Javascript :: first letter of each word in a sentence to uppercase javascript 
Javascript :: jquery validation manually trigger 
Javascript :: concat object 
Javascript :: restfull api methods 
Javascript :: javascript global variable across files 
Javascript :: fadein fadeout jquery 
Javascript :: vue dynamic route push with params 
Javascript :: count number of word in javascript 
Javascript :: insert into array js 
Javascript :: check checkbox based on value using jquery 
Javascript :: vue axios catch error 
Javascript :: react native open gmail app 
Javascript :: for open new tab we are using window.open but new tab are open in left side how to change the right side 
Javascript :: vue 3 computed 
Javascript :: return elemnt from array 
Javascript :: javascript isalphanumeric 
Javascript :: how to update the react version in next js app 
Javascript :: express response setTimeout 
Javascript :: javascript input prompt example 
Javascript :: multiple import react js 
Javascript :: make multiple function calls at the same time js async 
Javascript :: 12 hours to 24 hours javascript 
Javascript :: discord.js edit message by id 
Javascript :: disable a button react 
Javascript :: jspdf pdf from html 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =