Answers for "file upload html firebase"

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

file upload html firebase

<!DOCTYPE html>
<html>
<head>
	<title>Firebase Web Basics</title>

	<link href="https://fonts.googleapis.com/css?family=Raleway:300,400,500,700" rel="stylesheet">

	<script src="https://use.fontawesome.com/939e9dd52c.js"></script>

	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

	<div class="mainDiv" align="right">
		<h1 align="left">Firebase File Upload</h1>
		<progress id="uploader" value="0" max="100">0%</progress>
		<input type="file" id="fileButton" value="upload"/>
	</div>



<script src="https://www.gstatic.com/firebasejs/3.7.4/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "******************************",
    authDomain: "******************************",
    databaseURL: "******************************",
    storageBucket: "******************************",
    messagingSenderId: "******************************"
  };
  firebase.initializeApp(config);
  //-------------------------------------
  
  var uploader = document.getElementById('uploader');
  var fileButton =         document.getElementById('fileButton');
  fileButton.addEventListener('change', function(e){
  var file = e.target.files[0];
  var storageRef = firebase.storage().ref('img/'+file.name);
  var task = storageRef.put(file);
  task.on('state_changed', function progress(snapshot) {
    var percentage = (snapshot.bytesTransferred/snapshot.totalBytes)*100;
    uploader.value = percentage;

  }, function error(err) {


  },function complete() {

  });
});  
  
  
</script>

<script src="fileup.js">
</script>
</body>
</html>
Posted by: Guest on September-29-2020

Browse Popular Code Answers by Language