├── .env
├── public
├── robots.txt
├── favicon.ico
├── logo192.png
├── logo512.png
├── manifest.json
└── index.html
├── src
├── http-common.js
├── setupTests.js
├── index.css
├── reportWebVitals.js
├── App.js
├── services
│ └── upload-files.service.js
├── index.js
├── App.css
└── components
│ └── upload-files.component.js
├── .gitignore
├── package.json
├── .eslintcache
└── README.md
/.env:
--------------------------------------------------------------------------------
1 | PORT=8081
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bezkoder/material-ui-file-upload/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bezkoder/material-ui-file-upload/HEAD/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bezkoder/material-ui-file-upload/HEAD/public/logo512.png
--------------------------------------------------------------------------------
/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/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 |
--------------------------------------------------------------------------------
/.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 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/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/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/App.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import "./App.css";
3 | import { Typography } from "@material-ui/core";
4 |
5 | import UploadFiles from "./components/upload-files.component";
6 |
7 | function App() {
8 | return (
9 |
10 |
11 | bezkoder.com
12 | Material UI File Upload
13 |
14 |
15 |
16 |
17 | );
18 | }
19 |
20 | export default App;
21 |
--------------------------------------------------------------------------------
/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/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .mg20 {
2 | margin: 20px;
3 | }
4 |
5 | .mb25 {
6 | margin-bottom: 25px;
7 | }
8 |
9 | .container {
10 | position: relative;
11 | width: 600px;
12 | margin: auto;
13 | }
14 |
15 | .btn-upload {
16 | position: absolute !important;
17 | right: 10px;
18 | }
19 |
20 | .file-name {
21 | overflow: hidden;
22 | text-overflow: ellipsis;
23 | white-space: nowrap;
24 | display: inline-block;
25 | max-width: 300px;
26 | vertical-align: middle;
27 | }
28 |
29 | .list-group {
30 | padding: 0;
31 | margin: 0;
32 | }
33 |
34 | .list-header {
35 | margin-top: 10px !important;
36 | }
37 |
38 | .upload-message {
39 | font-weight: bold;
40 | color: #63A84E;
41 | margin-top: 20px !important;
42 | }
43 |
44 | .upload-message.error{
45 | color: #DA4148;
46 | }
47 |
48 | .btn-choose {
49 | margin-right: 10px !important;
50 | }
51 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "material-ui-file-upload",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@material-ui/core": "^4.11.1",
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.0",
11 | "react": "^17.0.1",
12 | "react-dom": "^17.0.1",
13 | "react-scripts": "4.0.1",
14 | "web-vitals": "^0.2.4"
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 |
--------------------------------------------------------------------------------
/.eslintcache:
--------------------------------------------------------------------------------
1 | [{"D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\reportWebVitals.js":"1","D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\services\\upload-files.service.js":"2","D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\components\\upload-files.component.js":"3","D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\App.js":"4"},{"size":362,"mtime":1606707863160,"results":"5","hashOfConfig":"6"},{"size":440,"mtime":1606708385522,"results":"7","hashOfConfig":"6"},{"size":4045,"mtime":1607137703003,"results":"8","hashOfConfig":"6"},{"size":453,"mtime":1607142929967,"results":"9","hashOfConfig":"6"},{"filePath":"10","messages":"11","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"18fhyyc",{"filePath":"12","messages":"13","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"14","messages":"15","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"16","messages":"17","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\reportWebVitals.js",[],"D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\services\\upload-files.service.js",[],"D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\components\\upload-files.component.js",[],"D:\\Projects\\ReactProjects\\material-ui-file-upload\\src\\App.js",[]]
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Material UI File Upload example with Axios & Progress Bar
2 |
3 | For instruction, please visit:
4 | > [Material UI File Upload example with Axios & Progress Bar](https://bezkoder.com/material-ui-file-upload/)
5 |
6 | Rest APIs server for this React Client:
7 | > [Node.js Express File Upload Rest API example](https://bezkoder.com/node-js-express-file-upload/)
8 |
9 | > [Spring Boot Multipart File upload example](https://bezkoder.com/spring-boot-file-upload/)
10 |
11 | More Practice:
12 | > [React Material UI examples with a CRUD Application](https://bezkoder.com/react-material-ui-examples-crud/)
13 |
14 | > [React Pagination with API using Material-UI](https://bezkoder.com/react-pagination-material-ui/)
15 |
16 | > [React File Upload with Axios & Boostrap Progress Bar](https://bezkoder.com/react-file-upload-axios/)
17 |
18 | > [React (with Hooks) File Upload with Axios & Boostrap Progress Bar](https://bezkoder.com/react-hooks-file-upload/)
19 |
20 | ## Fullstack CRUD
21 | With Node.js Express:
22 |
23 | > [React.js + Node.js Express + MySQL](https://bezkoder.com/react-node-express-mysql/)
24 |
25 | > [React.js + Node.js Express + PostgreSQL](https://bezkoder.com/react-node-express-postgresql/)
26 |
27 | > [React.js + Node.js Express + MongoDB](https://bezkoder.com/react-node-express-mongodb-mern-stack/)
28 |
29 | With Spring Boot:
30 |
31 | > [React.js + Spring Boot + MySQL](https://bezkoder.com/react-spring-boot-crud/)
32 |
33 | > [React.js + Spring Boot + PostgreSQL](https://bezkoder.com/spring-boot-react-postgresql/)
34 |
35 | > [React.js + Spring Boot + MongoDB](https://bezkoder.com/react-spring-boot-mongodb/)
36 |
37 | With Django:
38 |
39 | > [React.js + Django Rest Framework](https://bezkoder.com/django-react-axios-rest-framework/)
40 |
41 | ## Serverless
42 | > [React Firebase CRUD App with Realtime Database](https://bezkoder.com/react-firebase-crud/)
43 |
44 | > [React Firestore CRUD App example | Firebase Cloud Firestore](https://bezkoder.com/react-firestore-crud/)
45 |
46 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
47 |
48 | ### Set port
49 | .env
50 | ```
51 | PORT=8081
52 | ```
53 |
54 | ## Project setup
55 |
56 | In the project directory, you can run:
57 |
58 | ```
59 | npm install
60 | # or
61 | yarn install
62 | ```
63 |
64 | or
65 |
66 | ### Compiles and hot-reloads for development
67 |
68 | ```
69 | npm start
70 | # or
71 | yarn start
72 | ```
73 |
74 | Open [http://localhost:8081](http://localhost:8081) to view it in the browser.
75 |
76 | The page will reload if you make edits.
77 |
--------------------------------------------------------------------------------
/src/components/upload-files.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 UploadFiles 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 | selectedFiles: undefined,
29 | currentFile: undefined,
30 | progress: 0,
31 | message: "",
32 | isError: false,
33 | fileInfos: [],
34 | };
35 | }
36 |
37 | componentDidMount() {
38 | UploadService.getFiles().then((response) => {
39 | this.setState({
40 | fileInfos: response.data,
41 | });
42 | });
43 | }
44 |
45 | selectFile(event) {
46 | this.setState({
47 | selectedFiles: event.target.files,
48 | });
49 | }
50 |
51 | upload() {
52 | let currentFile = this.state.selectedFiles[0];
53 |
54 | this.setState({
55 | progress: 0,
56 | currentFile: currentFile,
57 | });
58 |
59 | UploadService.upload(currentFile, (event) => {
60 | this.setState({
61 | progress: Math.round((100 * event.loaded) / event.total),
62 | });
63 | })
64 | .then((response) => {
65 | this.setState({
66 | message: response.data.message,
67 | isError: false
68 | });
69 | return UploadService.getFiles();
70 | })
71 | .then((files) => {
72 | this.setState({
73 | fileInfos: files.data,
74 | });
75 | })
76 | .catch(() => {
77 | this.setState({
78 | progress: 0,
79 | message: "Could not upload the file!",
80 | currentFile: undefined,
81 | isError: true
82 | });
83 | });
84 |
85 | this.setState({
86 | selectedFiles: undefined,
87 | });
88 | }
89 |
90 | render() {
91 | const {
92 | selectedFiles,
93 | currentFile,
94 | progress,
95 | message,
96 | fileInfos,
97 | isError
98 | } = this.state;
99 |
100 | return (
101 |
102 | {currentFile && (
103 |
104 |
105 |
106 |
107 |
108 | {`${progress}%`}
109 |
110 | )
111 | }
112 |
113 |
127 |
128 | {selectedFiles && selectedFiles.length > 0 ? selectedFiles[0].name : null}
129 |
130 |
139 |
140 |
141 | {message}
142 |
143 |
144 |
145 | List of Files
146 |
147 |
148 | {fileInfos &&
149 | fileInfos.map((file, index) => (
150 |
153 | {file.name}
154 |
155 | ))}
156 |
157 |
158 | );
159 | }
160 | }
161 |
--------------------------------------------------------------------------------