Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29cd566

Browse files
committed
Upload percentage in progress bar
1 parent f61d655 commit 29cd566

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

firebase-storage-react/src/App.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
width: 100%;
1818
}
1919

20+
.app__progress {
21+
width: 100%;
22+
height: 10px;
23+
background-color: #4caf50;
24+
position: relative;
25+
margin-top: 10px;
26+
}
27+
2028
.app {
2129
height: 100vh;
2230
display: flex;

firebase-storage-react/src/App.js

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,75 @@
1-
import { getDownloadURL, ref, uploadBytes } from 'firebase/storage';
2-
import './App.css';
3-
import { storage } from './firebase';
1+
import {
2+
getDownloadURL,
3+
ref,
4+
uploadBytes,
5+
uploadBytesResumable,
6+
} from "firebase/storage";
7+
import { useState } from "react";
48

9+
import "./App.css";
10+
import { storage } from "./firebase";
511

612
export default function App() {
7-
const handleSubmit = async (e) => {
8-
e.preventDefault();
9-
const file = e.target[0]?.files[0];
10-
if (!file) return null;
13+
const [progressPercent, setProgressPercent] = useState(0);
1114

12-
const storageRef = ref(storage, `files/${file.name}`);
13-
const snapshot = await uploadBytes(storageRef, file);
14-
// console.log(snapshot);
15-
if (snapshot) {
16-
e.target[0].value = null;
17-
const url = await getDownloadURL(storageRef)
18-
console.log(url);
19-
}
20-
}
15+
const handleSubmit = async (e) => {
16+
e.preventDefault();
17+
const file = e.target[0]?.files[0];
18+
console.log(`Handle Submit`, e.target[0].files);
19+
if (!file) return null;
2120

22-
return (
23-
<div className='App'>
24-
<div className="app" name="upload_file">
25-
<form className='app__form' onSubmit={handleSubmit}>
26-
<input type="file" />
27-
<button type="submit" className="button" onClick={e => e.preventDefault()}>Upload</button>
28-
</form>
29-
</div>
30-
</div >
31-
);
21+
const storageRef = ref(storage, `files/${file.name}`);
22+
// const snapshot = await uploadBytes(storageRef, file);
23+
24+
// console.log(snapshot);
25+
// if (snapshot) {
26+
// e.target[0].value = null;
27+
// const url = await getDownloadURL(storageRef);
28+
// console.log(url);
29+
// }
30+
31+
const uploadTask = uploadBytesResumable(storageRef, file);
32+
uploadTask.on(
33+
"state_changed",
34+
(snapshot) => {
35+
const progress = Math.round(
36+
(snapshot.bytesTransferred / snapshot.totalBytes) * 100
37+
);
38+
setProgressPercent(progress);
39+
},
40+
(error) => {
41+
switch (error.code) {
42+
case `storage/cancelled`:
43+
alert(`Upload cancelled`);
44+
break;
45+
default:
46+
alert(error);
47+
}
48+
},
49+
() => {
50+
e.target[0].value = "";
51+
getDownloadURL(storageRef).then((downloadURL) => {
52+
console.log(downloadURL);
53+
});
54+
}
55+
);
56+
};
57+
58+
return (
59+
<div className="App">
60+
<div className="app" name="upload_file">
61+
<form className="app__form" onSubmit={handleSubmit}>
62+
<input type="file" />
63+
<progress
64+
className="app__progress"
65+
value={progressPercent}
66+
max="100"
67+
/>
68+
<button type="submit" className="button">
69+
Upload
70+
</button>
71+
</form>
72+
</div>
73+
</div>
74+
);
3275
}

firebase-storage-react/src/firebase.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { getStorage } from "firebase/storage";
44

55
// Your web app's Firebase configuration
66
const firebaseConfig = {
7-
apiKey: "AIzaSyC5GB1ONo0cr4qAqZ9heNlaCLz6YWm2ZlM",
8-
authDomain: "firestore-react-tut.firebaseapp.com",
9-
projectId: "firestore-react-tut",
10-
storageBucket: "firestore-react-tut.appspot.com",
11-
messagingSenderId: "729522566387",
12-
appId: "1:729522566387:web:c5c73bcddefcaab175b386",
7+
apiKey: "AIzaSyB5DrC2FCtPhuFQ7kdxkt9TP0MBTq_Dwh0",
8+
authDomain: "fir-storage-react-6fc1b.firebaseapp.com",
9+
projectId: "fir-storage-react-6fc1b",
10+
storageBucket: "fir-storage-react-6fc1b.appspot.com",
11+
messagingSenderId: "348005102029",
12+
appId: "1:348005102029:web:7fa794cdf3ab020b801484"
1313
};
1414

1515
// Initialize Firebase

0 commit comments

Comments
 (0)