Answers for "how to upload files to firebase storage"

1

firebase upload file

const [imagetoPost, setImagetoPost] = useState(null);

const sendPost = (e) => {
	e.preventDefault();
	if (!post) return null;
	db.collection('posts')
		.add({
			// whatever you want to add other than file
		})
		.then((doc) => {
			if (imagetoPost) {
				// upload stuff
				const uploadTask = storage
					.ref(`posts/${doc.id}`)
					.putString(imagetoPost, 'data_url');

					// OPTIONAL: here you can remove the file from state

				uploadTask.on(
					'state_change',
					null,
					(error) => console.error(error),
					() => {
						// when the upload completes
						storage
							.ref(`posts/${doc.id}`)
							.getDownloadURL()
							.then((url) => {
								db.collection('posts').doc(doc.id).set(
									{ postImage: url },
									{ merge: true }
								);
							});
					}
				);
			}
		});
};

const addPostImage = (e) => {
	const reader = new FileReader();
	if (e.target.files[0]) {
		reader.readAsDataURL(e.target.files[0]);
	}

	reader.onload = (readerEvent) => {
		setImagetoPost(readerEvent.target.result);
	};
};
Posted by: Guest on June-15-2021
0

how to upload files to firebase storage

async function uploadBlob(file: Blob | Uint8Array | ArrayBuffer, path: string) {
  try {
    const uploadTaskSnapShot = await storage.ref(path).put(file);
    let url: string = await uploadTaskSnapShot.ref.getDownloadURL();
    return url;
  } catch (error) {
    throw new Error("Could not upload file");
  }
}
Posted by: Guest on September-07-2021

Code answers related to "how to upload files to firebase storage"

Browse Popular Code Answers by Language