“Unggah file di React” Kode Jawaban

Unggah file di 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

Unggah gambar bereaksi

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

Jawaban yang mirip dengan “Unggah file di React”

Pertanyaan yang mirip dengan “Unggah file di React”

Lebih banyak jawaban terkait untuk “Unggah file di React” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya