├── .env ├── .gitignore ├── README.md ├── material-ui-image-upload-preview-react-example.png ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.js ├── components │ └── upload-images.component.js ├── http-common.js ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.js ├── services │ └── upload-files.service.js └── setupTests.js └── yarn.lock /.env: -------------------------------------------------------------------------------- 1 | PORT=8081 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | .eslintcache 21 | 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Material UI Image Upload example with Preview, Axios & Progress Bar 2 | Build Material UI Image Upload example with Preview to Rest API. The React App uses [Axios](https://github.com/axios/axios) and [Multipart](https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html) File for making HTTP requests, Material UI for progress bar and other UI components. You also have a display list of images' information (with download url). 3 | 4 | We're gonna create a React Material UI Image upload application in that user can: 5 | - display the preview of image before uploading 6 | - see the upload process (percentage) with progress bar 7 | - view all uploaded images 8 | - link to download the image when clicking on the file name 9 | 10 | ![material-ui-image-upload-preview-react-example](material-ui-image-upload-preview-react-example.png) 11 | 12 | For instruction, please visit: 13 | > [Material UI Image Upload example with Preview, Axios & Progress Bar](https://bezkoder.com/material-ui-image-upload/) 14 | 15 | Rest APIs server for this React Client: 16 | > [Node.js Express File Upload Rest API example](https://bezkoder.com/node-js-express-file-upload/) 17 | 18 | > [Spring Boot Multipart File upload example](https://bezkoder.com/spring-boot-file-upload/) 19 | 20 | More Practice: 21 | > [React Material UI examples with a CRUD Application](https://bezkoder.com/react-material-ui-examples-crud/) 22 | 23 | > [React Pagination with API using Material-UI](https://bezkoder.com/react-pagination-material-ui/) 24 | 25 | > [React File Upload with Axios & Boostrap Progress Bar](https://bezkoder.com/react-file-upload-axios/) 26 | 27 | > [React (with Hooks) File Upload with Axios & Boostrap Progress Bar](https://bezkoder.com/react-hooks-file-upload/) 28 | 29 | > [React JWT Authentication & Authorization example](https://bezkoder.com/react-jwt-auth/) 30 | 31 | > [React + Redux: JWT Authentication & Authorization example](https://bezkoder.com/react-redux-jwt-auth/) 32 | 33 | ## Fullstack CRUD 34 | With Node.js Express: 35 | 36 | > [React.js + Node.js Express + MySQL](https://bezkoder.com/react-node-express-mysql/) 37 | 38 | > [React.js + Node.js Express + PostgreSQL](https://bezkoder.com/react-node-express-postgresql/) 39 | 40 | > [React.js + Node.js Express + MongoDB](https://bezkoder.com/react-node-express-mongodb-mern-stack/) 41 | 42 | With Spring Boot: 43 | 44 | > [React.js + Spring Boot + MySQL](https://bezkoder.com/react-spring-boot-crud/) 45 | 46 | > [React.js + Spring Boot + PostgreSQL](https://bezkoder.com/spring-boot-react-postgresql/) 47 | 48 | > [React.js + Spring Boot + MongoDB](https://bezkoder.com/react-spring-boot-mongodb/) 49 | 50 | With Django: 51 | 52 | > [React.js + Django Rest Framework](https://bezkoder.com/django-react-axios-rest-framework/) 53 | 54 | ## Serverless 55 | > [React Firebase CRUD App with Realtime Database](https://bezkoder.com/react-firebase-crud/) 56 | 57 | > [React Firestore CRUD App example | Firebase Cloud Firestore](https://bezkoder.com/react-firestore-crud/) 58 | 59 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 60 | 61 | ### Set port 62 | .env 63 | ``` 64 | PORT=8081 65 | ``` 66 | 67 | ## Project setup 68 | 69 | In the project directory, you can run: 70 | 71 | ``` 72 | npm install 73 | # or 74 | yarn install 75 | ``` 76 | 77 | or 78 | 79 | ### Compiles and hot-reloads for development 80 | 81 | ``` 82 | npm start 83 | # or 84 | yarn start 85 | ``` 86 | 87 | Open [http://localhost:8081](http://localhost:8081) to view it in the browser. 88 | 89 | The page will reload if you make edits. 90 | -------------------------------------------------------------------------------- /material-ui-image-upload-preview-react-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/material-ui-image-upload/eb0a7f625f1bd2140010b38e50f035bfa42029d7/material-ui-image-upload-preview-react-example.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "material-ui-image-upload", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@material-ui/core": "^4.11.3", 7 | "@testing-library/jest-dom": "^5.11.4", 8 | "@testing-library/react": "^11.1.0", 9 | "@testing-library/user-event": "^12.1.10", 10 | "axios": "^0.21.1", 11 | "react": "^17.0.1", 12 | "react-dom": "^17.0.1", 13 | "react-scripts": "4.0.3", 14 | "web-vitals": "^1.0.1" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": [ 24 | "react-app", 25 | "react-app/jest" 26 | ] 27 | }, 28 | "browserslist": { 29 | "production": [ 30 | ">0.2%", 31 | "not dead", 32 | "not op_mini all" 33 | ], 34 | "development": [ 35 | "last 1 chrome version", 36 | "last 1 firefox version", 37 | "last 1 safari version" 38 | ] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/material-ui-image-upload/eb0a7f625f1bd2140010b38e50f035bfa42029d7/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/material-ui-image-upload/eb0a7f625f1bd2140010b38e50f035bfa42029d7/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/material-ui-image-upload/eb0a7f625f1bd2140010b38e50f035bfa42029d7/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .mg20 { 2 | margin: 20px; 3 | } 4 | 5 | .my20 { 6 | margin-top: 20px; 7 | margin-bottom: 20px; 8 | } 9 | 10 | .mr20 { 11 | margin-right: 20px; 12 | } 13 | 14 | .container { 15 | position: relative; 16 | width: 600px; 17 | margin: auto; 18 | } 19 | 20 | .preview { 21 | max-width: 200px; 22 | } 23 | 24 | .btn-upload { 25 | position: absolute !important; 26 | right: 10px; 27 | } 28 | 29 | .file-name { 30 | overflow: hidden; 31 | text-overflow: ellipsis; 32 | white-space: nowrap; 33 | display: inline-block; 34 | max-width: 300px; 35 | vertical-align: middle; 36 | } 37 | 38 | .list-group { 39 | padding: 0; 40 | margin: 0; 41 | } 42 | 43 | .list-header { 44 | margin-top: 10px !important; 45 | } 46 | 47 | .upload-message { 48 | font-weight: bold; 49 | color: #63A84E; 50 | } 51 | 52 | .upload-message.error{ 53 | color: #DA4148; 54 | } 55 | 56 | .btn-choose { 57 | margin-right: 10px !important; 58 | } 59 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import "./App.css"; 3 | import { Typography } from "@material-ui/core"; 4 | 5 | import UploadImages from "./components/upload-images.component"; 6 | 7 | function App() { 8 | return ( 9 |
10 |
11 | bezkoder.com 12 | Material UI Image Upload with Preview 13 |
14 | 15 | 16 |
17 | ); 18 | } 19 | 20 | export default App; 21 | -------------------------------------------------------------------------------- /src/components/upload-images.component.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import LinearProgress from '@material-ui/core/LinearProgress'; 3 | import { Box, Typography, Button, ListItem, withStyles } from '@material-ui/core'; 4 | 5 | import UploadService from "../services/upload-files.service"; 6 | 7 | const BorderLinearProgress = withStyles((theme) => ({ 8 | root: { 9 | height: 15, 10 | borderRadius: 5, 11 | }, 12 | colorPrimary: { 13 | backgroundColor: "#EEEEEE", 14 | }, 15 | bar: { 16 | borderRadius: 5, 17 | backgroundColor: '#1a90ff', 18 | }, 19 | }))(LinearProgress); 20 | 21 | export default class UploadImages extends Component { 22 | constructor(props) { 23 | super(props); 24 | this.selectFile = this.selectFile.bind(this); 25 | this.upload = this.upload.bind(this); 26 | 27 | this.state = { 28 | currentFile: undefined, 29 | previewImage: undefined, 30 | progress: 0, 31 | 32 | message: "", 33 | isError: false, 34 | imageInfos: [], 35 | }; 36 | } 37 | 38 | componentDidMount() { 39 | UploadService.getFiles().then((response) => { 40 | this.setState({ 41 | imageInfos: response.data, 42 | }); 43 | }); 44 | } 45 | 46 | selectFile(event) { 47 | this.setState({ 48 | currentFile: event.target.files[0], 49 | previewImage: URL.createObjectURL(event.target.files[0]), 50 | progress: 0, 51 | message: "" 52 | }); 53 | } 54 | 55 | upload() { 56 | this.setState({ 57 | progress: 0 58 | }); 59 | 60 | UploadService.upload(this.state.currentFile, (event) => { 61 | this.setState({ 62 | progress: Math.round((100 * event.loaded) / event.total), 63 | }); 64 | }) 65 | .then((response) => { 66 | this.setState({ 67 | message: response.data.message, 68 | isError: false 69 | }); 70 | return UploadService.getFiles(); 71 | }) 72 | .then((files) => { 73 | this.setState({ 74 | imageInfos: files.data, 75 | }); 76 | }) 77 | .catch((err) => { 78 | this.setState({ 79 | progress: 0, 80 | message: "Could not upload the image!", 81 | currentFile: undefined, 82 | isError: true 83 | }); 84 | }); 85 | } 86 | 87 | render() { 88 | const { 89 | currentFile, 90 | previewImage, 91 | progress, 92 | message, 93 | imageInfos, 94 | isError 95 | } = this.state; 96 | 97 | return ( 98 |
99 | 114 |
115 | {currentFile ? currentFile.name : null} 116 |
117 | 126 | 127 | {currentFile && ( 128 | 129 | 130 | 131 | 132 | 133 | {`${progress}%`} 134 | 135 | ) 136 | } 137 | 138 | {previewImage && ( 139 |
140 | 141 |
142 | )} 143 | 144 | {message && ( 145 | 146 | {message} 147 | 148 | )} 149 | 150 | 151 | List of Images 152 | 153 | 164 |
165 | ); 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /src/http-common.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | 3 | export default axios.create({ 4 | baseURL: "http://localhost:8080", 5 | headers: { 6 | "Content-type": "application/json" 7 | } 8 | }); -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/services/upload-files.service.js: -------------------------------------------------------------------------------- 1 | import http from "../http-common"; 2 | 3 | class UploadFilesService { 4 | upload(file, onUploadProgress) { 5 | let formData = new FormData(); 6 | 7 | formData.append("file", file); 8 | 9 | return http.post("/upload", formData, { 10 | headers: { 11 | "Content-Type": "multipart/form-data", 12 | }, 13 | onUploadProgress, 14 | }); 15 | } 16 | 17 | getFiles() { 18 | return http.get("/files"); 19 | } 20 | } 21 | 22 | export default new UploadFilesService(); 23 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | --------------------------------------------------------------------------------