diff options
author | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-26 21:21:03 +0530 |
---|---|---|
committer | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-26 21:21:03 +0530 |
commit | 55aed6d7c2df0daedbdabea0d1727acb1815ce2b (patch) | |
tree | 42dabdc4f68bd01867cd84c4a9beaa6a14ffb9c2 /src/components/UploadForm.jsx | |
parent | 4833cc911b004b3da476863ddf27e09d93dd89c0 (diff) | |
download | admin-panel-55aed6d7c2df0daedbdabea0d1727acb1815ce2b.tar.gz admin-panel-55aed6d7c2df0daedbdabea0d1727acb1815ce2b.tar.bz2 admin-panel-55aed6d7c2df0daedbdabea0d1727acb1815ce2b.zip |
added upload feature
Diffstat (limited to 'src/components/UploadForm.jsx')
-rw-r--r-- | src/components/UploadForm.jsx | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/components/UploadForm.jsx b/src/components/UploadForm.jsx new file mode 100644 index 0000000..9bb16e7 --- /dev/null +++ b/src/components/UploadForm.jsx @@ -0,0 +1,45 @@ +import React, { useState } from 'react'; +import { ref, uploadBytesResumable, getDownloadURL } from 'firebase/storage'; +import { storage } from '../firebase'; + +const UploadForm = ({ onUploadComplete }) => { + const [file, setFile] = useState(null); + const [progress, setProgress] = useState(0); + + const handleFileChange = (e) => { + setFile(e.target.files[0]); + }; + + const handleUpload = () => { + if (!file) return; + + const storageRef = ref(storage, `ads/${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) => { + onUploadComplete(downloadURL); + }); + } + ); + }; + + return ( + <div> + <input type="file" onChange={handleFileChange} /> + <button onClick={handleUpload}>Upload</button> + <div>Upload Progress: {progress}%</div> + </div> + ); +}; + +export default UploadForm; |