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 file in react

const upload = (e) => {
	console.warn(e.target.files)
	const files = e.target.files
	const formData = new FormData()
	formData.append('img', files[0])
	fetch('http://127.0.0.1:8000/api/store', {
		method: 'POST',
		body: formData,
	}).then((resp) => {
		resp.json().then((result) => {
			console.warn(result)
		})
	})
}
return(<div>
	<h1>Upload File in React js</h1>
	<input type='file' onChange={(e) => upload(e)} name='img' />
</div>)
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

react upload image

// 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 :: next router push 
Javascript :: change navigation animation react native 
Javascript :: what is jsx in react 
Javascript :: i18n vue cli 
Javascript :: javascript if value is a string function 
Javascript :: nodejs bodyparser form data 
Javascript :: get value for radio button in jquery label 
Javascript :: foreach break js 
Javascript :: how to remove key value pair from object in javascript 
Javascript :: count down timer in react native 
Javascript :: random number generator javascript with range 
Javascript :: node.js util module 
Javascript :: nextjs global scss variables 
Javascript :: email regular expression javascript 
Javascript :: js add text after div 
Javascript :: javascript Swapping Variables 
Javascript :: dm command discord.js 
Javascript :: javascript onclick button 
Javascript :: js key value array 
Javascript :: postman response xml json xml2Json 
Javascript :: bootstap jquery 
Javascript :: how to concatenate strings and variables in javascript 
Javascript :: form data object 
Javascript :: javascript round number to 5 decimal places 
Javascript :: string normalize javascript 
Javascript :: how to handle fetch errors 
Javascript :: add checkbox dynamically in javascript 
Javascript :: mongodb find all that dont have property 
Javascript :: how to add items in an array in js 
Javascript :: how to minimize electron app to tray icon 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =