这是我从哪里得到错误的文本。该代码位于函数sendData()之后。开头为:
var file = this.files[0];
我想确定按钮将文件发送到存储的位置,而不是当它检测到状态已更改时
var database = firebase.database();
var uploader = document.getElementById("uploader");
var fileButton = document.getElementById("fileButton").value;
var title = document.getElementById("title").value;
var description = document.getElementById("description").value;
var d = new Date();
var month = d.getMonth();
var day = d.getDay();
var hour = d.getHours();
var minute = d.getMinutes();
firebase.auth().onAuthStateChanged(function(user) {
if(user) {
var currentUser = document.getElementById('currentUser').href = "#";
var currentUser = document.getElementById('currentUser').innerHTML = "Cuenta";
} else {
var currentUser = document.getElementById('currentUser').href = "/auth/login.html";
var currentUser = document.getElementById('currentUser').innerHTML = "Inicio De Sesión";
window.location.href = "/auth/login.html";
}
});
function sendData() {
var file = this.files[0];
var storage = firebase.storage().ref("News/" + file.name);
var task = storage.put(file);
task.on('state_changed', function(snapshot) {
var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
uploader.value = percentage;
}, function error(err) {
console.log("Error: " + err);
alert("Error");
}, function() {
firebase.storage().ref("News/").child(file.name).getDownloadURL().then(function(url) {
var title = document.getElementById("title").value;
var description = document.getElementById("description").value;
var ref = database.ref("News");
var data = {
imageURL: url,
Title: title,
Description: description,
Date: month + "/" + day + " " + h + ":" + minutes
}
ref.push(data);
});
});
};
HTML:代码
<div class="mainContainer">
<h1>Database</h1>
<input id="title" placeholder="Título" type="text">
<textarea id="description" placeholder="Description"></textarea>
<br>
<progress id="uploader" value="0" max="100">
0%
</progress>
<br>
<input type="file" value="upload" id="fileButton" accept=".png, .mp4, .mp3, .pdf">
<button onclick="sendData()">Enviar</button>
</div>
答案 0 :(得分:-1)
可能this.files
是未定义的,并且您正在尝试访问索引为0
的元素。