blob: 531feda6794e7a4fae5646ade0f81dd94c48c0fb (
plain) (
tree)
|
|
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;
|