“Reactu de upload da imagem” Respostas de código

upload de imagem no 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;
Abhishek

Faça o upload do arquivo no 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>)
Abhishek

Reactu de upload da imagem

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;
Vivacious Vicuña

Como fazer upload de imagem no 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
Concerned Chimpanzee

Reactu de upload de imagens múltiplas

class ImageUpload extends React.Component {
  state = {
    files: []
  }

  fileSelectedHandler = (e) => {
    this.setState({ files: [...this.state.files, ...e.target.files] })
  }

  render() {
    return (
      <form>
        <div><h2>Upload images</h2></div>
        <h3>Images</h3>
        <input type="file" multiple onChange={this.fileSelectedHandler} />
      </form>
    )
  }
}


ReactDOM.render(<ImageUpload />, document.getElementById('app'))
Frantic Falcon

Respostas semelhantes a “Reactu de upload da imagem”

Perguntas semelhantes a “Reactu de upload da imagem”

Mais respostas relacionadas para “Reactu de upload da imagem” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código