我希望上传文件并打印下载网址 这是用于此的代码
<script>
var file = document.getElementById('file');
var storageRef = firebase.storage().ref();
// Create the file metadata
var metadata = {
contentType: 'image/jpeg'
};
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
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) {
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>
请帮助我纠正上载文件并显示下载网址的代码
答案 0 :(得分:0)
负责文件上传的表单元素应如下所示:
<form enctype="multipart/form-data" action="/upload/file" method="post">