aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Upload.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/Upload.js')
-rw-r--r--src/components/Upload.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/components/Upload.js b/src/components/Upload.js
new file mode 100644
index 0000000..6308fda
--- /dev/null
+++ b/src/components/Upload.js
@@ -0,0 +1,54 @@
+import React, { useState } from 'react';
+import { ref, uploadBytesResumable, getDownloadURL } from 'firebase/storage';
+import { storage } from '../firebase';
+
+function Upload() {
+ const [file, setFile] = useState(null);
+ const [progress, setProgress] = useState(0);
+ const [url, setUrl] = useState('');
+
+ const handleFileChange = (e) => {
+ if (e.target.files[0]) {
+ setFile(e.target.files[0]);
+ }
+ };
+
+ const handleUpload = () => {
+ if (!file) {
+ alert('Please choose a file first!');
+ return;
+ }
+
+ const storageRef = ref(storage, `uploads/${file.name}`);
+ const uploadTask = uploadBytesResumable(storageRef, file);
+
+ uploadTask.on(
+ 'state_changed',
+ (snapshot) => {
+ const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
+ setProgress(progress);
+ },
+ (error) => {
+ console.error('Upload failed', error);
+ },
+ () => {
+ getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
+ setUrl(downloadURL);
+ alert('File uploaded successfully!');
+ });
+ }
+ );
+ };
+
+ return (
+ <div className="Upload">
+ <h2>Upload Content</h2>
+ <input type="file" onChange={handleFileChange} />
+ <button onClick={handleUpload}>Upload</button>
+ <p>Upload Progress: {progress}%</p>
+ {url && <p>File URL: <a href={url} target="_blank" rel="noopener noreferrer">{url}</a></p>}
+ </div>
+ );
+}
+
+export default Upload;