diff options
Diffstat (limited to 'src/components/UploadForm.jsx')
-rw-r--r-- | src/components/UploadForm.jsx | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/components/UploadForm.jsx b/src/components/UploadForm.jsx new file mode 100644 index 0000000..531feda --- /dev/null +++ b/src/components/UploadForm.jsx @@ -0,0 +1,42 @@ +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 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) => { + // Observe state change events such as progress, pause, and resume + }, + (error) => { + console.error('Upload error:', error); + }, + () => { + // Handle successful uploads on complete + getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => { + onUploadComplete(downloadURL); + }); + } + ); + }; + + return ( + <div> + <input type="file" onChange={handleFileChange} /> + <button onClick={handleUpload}>Upload</button> + </div> + ); +}; + +export default UploadForm; |