|
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"; |
4 | 8 |
|
| 9 | +import "./App.css"; |
| 10 | +import { storage } from "./firebase"; |
5 | 11 |
|
6 | 12 | 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); |
11 | 14 |
|
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; |
21 | 20 |
|
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 | + ); |
32 | 75 | }
|
0 commit comments