Reputation: 11755
I am trying (for the first time) to upload an image on firebase/storage, but with a very limited success.
I tried to follow this document: https://firebase.google.com/docs/storage/web/upload-files
The code below is based on the paragraph titled Full Example at the bottom of the document.
But when I expect to see a image uploaded, I only get a ten bytes file, which is not displayable. And it shows an error: Error loading preview when I click on it in the firebase console.
Since something gets written (with the correct name: SoAf.jpg) I know I have no permission issue. But obviously I am not doing what I should to get the image uploaded correctly.
It would be great if someone could see what is wrong in this code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="">
<script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.5/firebase.js"></script>
</head>
<body>
<form action='UpldTest3.html' method='post' enctype='multipart/form-data'>
National flag: <input type='file' accept='image/*' name='photo' id='photo'><br/>
<input type='submit' name='ValNatFlag' style='font-size:20px' value='Submit Picture'><br/>
</form>
<script>
// Firebase Initialization :
var config = {
apiKey: "myyKeyyy",
authDomain: "......firebaseapp.com",
databaseURL: "https://......firebaseio.com",
projectId: "....",
storageBucket: "........appspot.com",
messagingSenderId: "........."
},
app = firebase.initializeApp(config),
db = firebase.firestore(app);
</script>
<script>
var file = new File(["fooFOO.jpg"], "SoAf.jpg", {type: "image/jpeg"});
// Create the file metadata
var metadata = {
contentType: 'image/jpeg'
};
var storageRef = firebase.storage().ref();
// Upload file and metadata to the object 'images/SoAf.jpg'
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
function(snapshot) {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function(error) {
// A full list of error codes is available at
// https://firebase.google.com/docs/storage/web/handle-errors
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
//...
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
}, function() {
// Upload completed successfully, now we can get the download URL
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
});
});
</script>
</body>
</html>
Upvotes: 0
Views: 417
Reputation: 181
You can just listen to file change and get the file
<img src="" id="imgPic" alt="" height="80px">
<input type='file' accept='image/*' name='photo' id='photo'>
This is the script
var fileToRead = document.getElementById("photo");
fileToRead.addEventListener("change", function(event) {
var image = document.getElementById("imgPic");
image.src = URL.createObjectURL(event.target.files[0]) ;
var tobeSaved = event.target.files[0] // save on storage
}, false);
Upvotes: 1