Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

How to upload an image in React JS?

Writer Sebastian Wright
<div className="mb-1"> Image <span className="font-css top">*</span> <div className=""> <input type="file" name="ImageStyle"/> </div>
</div>

This is the snippet i provided that i was using to pick the file from the device in react js, Using this i can select the file and that filename is also shown as well What i want is now to store this file on S3 or anywhere and get its URL from there and POST it to my server using fetch api call.

4 Answers

Upload the image from your file and display it on your page in react, you can also get the image object in the state when we select the image to display on the webpage you have to convert the image object to object using URL.createObjectURL(fileObject)

import React, { Component } from "react";
class DisplayImage extends Component { constructor(props) { super(props); this.state = { image: null }; // if we are using arrow function binding in not required // this.onImageChange = this.onImageChange.bind(this); } onImageChange = event => { if (event.target.files && event.target.files[0]) { let img = event.target.files[0]; this.setState({ image: URL.createObjectURL(img) }); } }; render() { return ( <div> <div> <div> <img src={this.state.image} /> <h1>Select Image</h1> <input type="file" name="myImage" onChange={this.onImageChange} /> </div> </div> </div> ); }
}
export default DisplayImage;
2

Upload and Display Image usign React Hook's

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;
1

If you want to upload image and post it to an API. Then you install react-image-uploader. It saves the image to your local port and also in your database by raising a POST request.

using react-uploady you can do this very easily:

import React from "react";
import Uploady from "@rpldy/uploady";
import UploadButton from "@rpldy/upload-button";
import UploadPreview from "@rpldy/upload-preview";
const filterBySize = (file) => { //filter out images larger than 5MB return file.size <= 5242880;
};
const App = () => ( <Uploady destination={{ url: "" }} fileFilter={filterBySize} accept="image/*" > <UploadButton /> <UploadPreview /> </Uploady>
);

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy